In this article you will learn how to remove a layer in several MXD files using Python.
It often happens that during the execution of a project in which you have been making different graphical outputs and/or maps that for some reason it becomes necessary to remove a layer used to represent certain spatial information (Shapefile, feature Class, among others), either by decision of the end customer, your immediate boss, because it was added in the beginning without being necessary, or the information no longer exists in the dataset, among the various reasons that I can glimpse at this time. The traditional solution would be as follows:
How to remove a layer from ArcGIS / ArcMap?
1) Open the MXD file.
2) Right click on the layer to be removed (a).
3) Click on the Remove option (b).
4) Save the MXD file.
5) Repeat the procedure for each MXD file.
In case the correction must be done on one or two files it is not a big deal, but if the correction must be done on dozens of images and/or maps in MXD format this can consume a lot of your time, next, I will show you how it can be much more efficient using Python code (Don't worry you won't need to know how to program to do it).
How to batch remove layers in multiple ArcGIS / ArcMap files using Python?
Step 1: Open a blank MXD file.
Step 2: Open the Python window in the Standard toolbar.
Step 3: Backup the original MXD files.
Step 4: Copy the following code into a notepad and in line #02 of the code replace the path C:\EJEMPLO\EJEMPLO with the path where the MXD files to be modified are stored.
Step 5: In line #08 of the code replace LAYER_NAME with the name of the layer to remove.
Step 6 (Optional): If it is necessary to remove more than one layer, replicate and add lines #08 and #09 below line #09 once for each additional layer to be removed; preserving the horizontal spacing of the original lines.
Step 7: Copy the modified code into the Python window and press the Enter key twice.
After a few seconds you will see how the modification date of the ArcMap files starts to update, and when you open them you will notice the removal of the layer in each MXD file.
I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog, as well as another article where I will explain how to add layers within a particular layer group. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your questions in the contact page or leave your comment, I will be attentive to respond.


