problem with arcpy.mapping.RemoveLayer

2717
1
01-18-2014 01:25 PM
Emilbrundage
New Contributor III
I'm trying to remove a layer from my map. this code:

    arcpy.AddMessage(str(arcpy.mapping.ListLayers (mxd, "", df)))
    arcpy.AddMessage(addstru)
    arcpy.mapping.RemoveLayer (df, addstru)
    arcpy.AddMessage(str(arcpy.mapping.ListLayers (mxd, "", df)))


gets me this result:

[<map layer u'StruxPts_v04'>, <map layer u'Land_Projects_Parcels_P6'>]
StruxPts_v04
[<map layer u'StruxPts_v04'>, <map layer u'Land_Projects_Parcels_P6'>]

As you can see, the layer 'StruxPts_v04' was not removed. When does this sort of thing occur?

Thanks!


EDIT: I'm thinking it has something to do with my searchcursor. addstru is searched, but I delete the cursor's variables prior to the code above.
Tags (2)
0 Kudos
1 Reply
ArkadiuszMatoszka
Occasional Contributor II
Hi,

Cursor shouldn't cause such behavior. I think there might be something with referencing layer you're trying to remove (addstru variable).
Can you try change code to:
arcpy.AddMessage(str(arcpy.mapping.ListLayers (mxd, "", df)))
arcpy.AddMessage(addstru)
arcpy.mapping.RemoveLayer(df, arcpy.mapping.ListLayers(mxd, 'StruxPts_v04)[0])
arcpy.AddMessage(str(arcpy.mapping.ListLayers (mxd, "", df)))


Regards
Arek
0 Kudos