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.
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.
Hello, I've used your script successfully but it only works with mxd files that contains a few layers. If I try to use it with an mxd containing a lot of layers, Python IDLE crashes by showing ==== RESTART: Shell ====. Is there a workaround for this?
I'm currently trying to remove the Basemap layer for my main MXD since it's causing my ArcGIS 10.8.1 to crash whenever I try to open it. I've already disabled GPU acceleration, but haven't worked so far.
Many thanks!