Remove layers from a project without opening

692
1
08-01-2018 08:33 AM
ALL
by
New Contributor

Hi!

Sometime, my project .mxd crash because a layer is not well supported. So I am wondering if it is possible to remove a layer from a project without opening it (ArcGIS Desktop).

Thanks for your answer.

0 Kudos
1 Reply
StephenM
Occasional Contributor II

You could use ArcPy to do that, and run the script in ArcCatalog, ArcMap, or in an IDE like IDLE (which comes installed with ArcGIS).

Some of the classes and functions you'd probably want to use :

MapDocument—Help | ArcGIS Desktop

ListDataFrames—Help | ArcGIS Desktop

ListLayers—Help | ArcGIS Desktop

RemoveLayer—Help | ArcGIS Desktop

Here's an example:

import arcpy

arcpy.env.workspace=r"C:\DirectoryWhere\MapDocument\IsLocated"

mxd = arcpy.mapping.MapDocument("This_MXD.mxd")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layers = arcpy.mapping.ListLayers(mxd,data_frame=df)

for lyr in layers:
   if(lyr.name == "remove_this_layer"):
      arcpy.mapping.RemoveLayer(df,lyr)

mxd.save()

del mxd

print "Done..."

Basically what the script does is reference the MXD, point to the desired data frame, remove the desired layer based on its name, save the MXD, then delete the reference to the MXD to clear the lock on the file.

0 Kudos