Remove all joins programatically

917
3
02-13-2012 02:37 PM
Status: In Product Plan
Labels (1)
ThomasTri1
New Contributor II

see http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000066000000

would be nice to have the option of removing ALL joins from a table or feature class programatically just like you can from the GUI instead of having to remove them individually.

Instead of passing in the name of the join as a string, use a '*' or '#' to represent all.

3 Comments
curtvprice

Here's a little function that does this. You could use this in an arcpy.mapping script or with the Calculate Value tool in Model Builder. Another approach I've used to drop joins in scripting is to delete the layer or table view and recreate it. (This approach may not work well in Model Builder or a script that operates on an existing layer as it would delete the layer from the map!)

def RemoveAllJoins(tbl):
    """Remove all joins from a layer or table view"""
    flds = [f.name for f in arcpy.ListFields(tbl)]
    wk = os.path.dirname(arcpy.Describe(tbl).catalogPath)
    joins = [arcpy.ParseFieldName(f, wk).split(", ")[-2] for f in flds]
    joins = list(set(joins)) # unique the list
    if joins[0] == "(null)":
        print("no join active")
    else:
        # remove base table name from list and remove all joins
        joins.remove(arcpy.Describe(tbl).baseName)
        for j in joins:
            arcpy.RemoveJoin_management(tbl, j)
            print("Removed join {}".format(j))
    return
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Skyler
by

For anyone referring back to this page, calling arcpy.management.RemoveJoin without the optional join_name parameter will remove all joins from the feature layer or table view.

RemoveJoin will throw ERROR 000229 if no join is present, so be sure to catch arcpy.ExecuteError if you want to handle the exception.

DrewFlater
Status changed to: In Product Plan

As stated above, running Remove Join with no join name specified will result in removing all joins, in ArcGIS Pro 3.0.