## =======================================================================
def RemoveAllJoins(theInputTable):
## Remove all joined tables.
try:
arcpy.AddMessage("Remove All Joins from " + theInputTable)
theJoinList = []
theDesc = arcpy.Describe(theInputTable)
if theDesc.DataType.upper() == "FEATURELAYER":
# If it's a shapefile, remove the extension from the name.
theFC = theDesc.Featureclass.Name.replace(".shp","")
else:
arcpy.AddMessage("\n ERROR: " + theInputTable + " is not a feature layer!\n")
return False
# Look at the fields for this layer. A full field name includes
# the table name, i.e., TABLE.FIELD, where TABLE matches the name
# of the feature class. However, if there is a Join on the table,
# the joined field names will be like JOINTABLE.FIELD, so we can
# look for those other table names which represent Joins.
theFields = theDesc.Fields
for theField in theFields:
FullName = theField.Name
# Get rid of spaces in field name components and split them into a list.
nameList = arcpy.ParseFieldName(FullName).replace(" ","").split(",")
theTableName = nameList[2]
if theTableName != theFC and not theTableName in theJoinList:
# Keep track of Joins that were removed; remove only once.
theJoinList.append(theTableName)
arcpy.AddMessage(" Removing join: " + theTableName)
arcpy.RemoveJoin_management(theInputTable, theTableName)
return True
except Exception as e:
arcpy.AddError(e.message)
return False
# ---------------------------------------------------------------------------
# RemoveJoins.py
# ---------------------------------------------------------------------------
import arcpy,sys
from arcpy.mapping import *
## =======================================================================
def RemoveAllJoins(theInputTable):
## Remove all joined tables.
try:
arcpy.AddMessage("Remove All Joins from " + theInputTable.name)
theJoinList = []
theDesc = arcpy.Describe(theInputTable)
if theDesc.DataType.upper() == "FEATURELAYER":
# If it's a shapefile, remove the extension from the name.
theFC = theDesc.Featureclass.Name.replace(".shp","")
else:
arcpy.AddMessage("\n ERROR: " + theInputTable + " is not a feature layer!\n")
return False
# Look at the fields for this layer. A full field name includes
# the table name, i.e., TABLE.FIELD, where TABLE matches the name
# of the feature class. However, if there is a Join on the table,
# the joined field names will be like JOINTABLE.FIELD, so we can
# look for those other table names which represent Joins.
theFields = theDesc.Fields
for theField in theFields:
FullName = theField.Name
# Get rid of spaces in field name components and split them into a list.
nameList = arcpy.ParseFieldName(FullName).replace(" ","").split(",")
theTableName = nameList[2]
if theTableName != theFC and not theTableName in theJoinList:
# Keep track of Joins that were removed; remove only once.
theJoinList.append(theTableName)
arcpy.AddMessage(" Removing join: " + theTableName)
arcpy.RemoveJoin_management(theInputTable, theTableName)
return True
except Exception as e:
#arcpy.AddError(e.message)
return False
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for item in df:
RemoveAllJoins(item)
Here's a little function to remove all joins. Also see (and vote up!) Remove all joins programatically
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