I am trying to use RemoveLayer within the mapping module, but seem to be getting mixed results. I do the following to add a layer to the map
mxd = arcpy.mapping.MapDocument("CURRENT") # define map document
lyr = arcpy.mapping.Layer(r"C:\test.lyr") # define layer object
df = arcpy.mapping.ListDataFrames(mxd) # list data frames
arcpy.mapping.AddLayer(df[0],lyr) # add layer
Now that the layer is added, I am trying to use arcpy.mapping.RemoveLayer
to remove the layer from the map. I do the following.
arcpy.mapping.RemoveLayer(df[0],lyr)
But this doesn't work. The layers still exist in the map. But if I try the following code where I list the layers and then loop through and use RemoveLayer, it works now. Why?
lyrlist = arcpy.mapping.ListLayers(mxd)
for i in lyrlist:
arcpy.mapping.RemoveLayer(df[0],i)
Is this because Layer objects don't work with RemoveLayer for some reason?