using arcpy, i would like to remove,from 50 mxd files, a specific unique value called "residence a" (exist in 3 layers) from the table of content.

enter image description here

so i will get this result:

enter image description here

The value of "residence a" is "70" in the attribute table (in field named "YEUD")

enter image description here

When i use this code:

import arcpy,os,sys
from arcpy import env

env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    lyr = arcpy.mapping.ListLayers(mxd, "*")[0]
    if lyr.symbologyType == "UNIQUE_VALUES":
        vals = lyr.symbology.classLabels
        for v in vals:
            if v == "residence a":
                print mxdname
                print lyr.name
                print ("1 in layer " + lyr.name)
                arcpy.mapping.RemoveLayer(df, v)        
del mxd

i get en error:

Project -.mxd
1 in layer mig1

Traceback (most recent call last):
  File "C:/Users/yaron.KAYAMOT/Desktop/remove UNIQUE_VALUES in lyr.py", line 18, in    <module>
    arcpy.mapping.RemoveLayer(df, v)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py", line 1845,     in RemoveLayer
    assert isinstance(remove_layer, Layer)

For disclosure, i asked this question in https://geonet.esri.com/thread/158715

  • 1
    The RemoveLayer function "provides the ability to remove a layer within a data frame in a map document (.mxd).", not a not a layer unique value. – artwork21 May 6 '15 at 12:25
  • Are the 3 layers always the same in the 50 mxds? – GISGe May 6 '15 at 12:34
  • no i choose some layers specific, but other layers are diffrents one each other.but residence a always is value "70" – newGIS May 6 '15 at 12:41
  • Then I don't think there is a simple solution with arcpy. You could, for each layer, list all field values different from 'residence a' and use this list for the classValues property of the layer, but the colours won't be maintained. See the second example at the bottom of the UniqueValuesSymbology help page. – GISGe May 6 '15 at 12:58
  • 1
    If the values are always the same, couldn't you just make a new .lyr file with the same symbology but remove the unique value of 70? Then you can use the arcpy.management.ApplySymbologyFromLayer() function to overwrite the symbology. – crmackey May 6 '15 at 13:28

Unfortunately, with arcpy this method (removeUniqueValue()) does not exist under the UniqueValuesSymbology class.

An alternative in arcpy would be:

  1. Manually remove the value within each unique layer and save out a layer file for each unique layer

  2. Iterate through each mxd and loop through each layer list to search for layers that need updated (using for loop and if conditional logic). Once you find a layer that needs updated, use the UpdateLayer function to apply the needed layer file to the layer that needs updated, then refresh the table of contents and save the map.

Your Answer

