2

I have a bunch of mxd's and I want to remove layer file from them. Each mxd has 2 and more data frame. I don't know why it does not remove.

I'm trying this code:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
for mxd in arcpy.ListFiles("*.mxd"):
    print mxd
    mapdoc = arcpy.mapping.MapDocument(r"C:\Project\\" + mxd)
    df = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[0]    
    df2 = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[1] 
    Layer1 = arcpy.mapping.Layer(r"F:\GIS\practical_shapefile\residence\residence.lyr")
    arcpy.mapping.RemoveLayer(df, Layer1)
    print 'remove'
    arcpy.mapping.RemoveLayer(df2, Layer1)
    print 'remove'
    mapdoc.save()
del mxd 

This prints out:

Project.mxd 
remove 
remove 
Project2.mxd 
remove 
remove 
Project3.mxd 
remove 
remove
6
  • Where do you import arcpy? What messages do you see when you run that precise code? Also, you are creating a new layer object from a *.lyr file, when I would have expected that you would have looked for the layer by name using ListLayers, or perhaps by its data source if the name was variable and you wanted any layers with that data source removed.
    – PolyGeo
    Nov 2, 2014 at 6:57
  • i get a message: Project.mxd remove remove Project2.mxd remove remove Project3.mxd remove remove
    – newGIS
    Nov 2, 2014 at 7:34
  • It seems that py actuate the code, but when I open the mxd's it don't change
    – newGIS
    Nov 2, 2014 at 7:37
  • I would not expect your code as it stands to modify your maps. What is the name of the layer that you want to remove from every data frame of every map in your folder?
    – PolyGeo
    Nov 2, 2014 at 7:42
  • the name is residence.lyr
    – newGIS
    Nov 2, 2014 at 7:57

3 Answers 3

3

This code is based on the example code in the RemoveLayer documentation and I've just modified it to look more like your variable and layer names:

for df in arcpy.mapping.ListDataFrames(mapdoc):
    for lyr in arcpy.mapping.ListLayers(mapdoc, "", df):
        if lyr.name.lower() == "residence":
            arcpy.mapping.RemoveLayer(df, lyr)

Using the above should work without much change to fit in with your code.

However, your "equivalent" code is currently:

df = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[0]    
df2 = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[1] 
Layer1 = arcpy.mapping.Layer(r"F:\GIS\practical_shapefile\residence\residence.lyr")
arcpy.mapping.RemoveLayer(df, Layer1)
print 'remove'
arcpy.mapping.RemoveLayer(df2, Layer1)

In your code the problems I see are:

  1. Where you set df and df2 you are looking for the first and second data frames called "Layers" in each map. I am pretty sure that your data frames will have different names.
  2. You've created a layer object called Layer1 from residence.lyr but you have not added this to your map so it cannot be removed, or used to remove something else.
  3. To get hold of layers to be deleted you will be better iterating through ListLayers to locate layers with your target name - as in the example code at the top.
0

this code remove layer file from MXDs by data source:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env


env.workspace = r"C:\Project"   
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            if lyr.dataSource == r"C:\Project\layers\New File Geodatabase.gdb\ways":
                arcpy.mapping.RemoveLayer(df, lyr)
                print 'remove'

    mxd.save()
del mxd
3
  • Technically this is removing a layer not a layer file. To remove a layer file from a map (*.mxd) does not make much sense because a layer file is a separate *.lyr file.
    – PolyGeo
    Nov 6, 2014 at 6:23
  • if you change the GDB with layer file it works
    – newGIS
    Nov 9, 2014 at 5:35
  • There is no layer file mentioned in your code. Your code is working with layers (layer objects), not layer files.
    – PolyGeo
    Nov 9, 2014 at 5:38
-1

and this code remove layer file from MXDs by layer name in the table of content:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env


env.workspace = r"F:\Projects\Guy\mikta3\gis"   
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"F:\Projects\Guy\mikta3\gis\\" + mxdname)
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            if lyr.name == "residence":
                arcpy.mapping.RemoveLayer(df, lyr)
                print 'remove'


    mxd.save()
del mxd
1
  • Technically this is removing a layer not a layer file. To remove a layer file from a map (*.mxd) does not make much sense because a layer file is a separate *.lyr file.
    – PolyGeo
    Nov 6, 2014 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.