AnsweredAssumed Answered

Remove layers-arcpy

Question asked by yaron148 on Dec 27, 2015
Latest reply on Jan 12, 2016 by xander_bakker

Expanding on my previous question Remove Layers ,

i try to remove layers from mxd's (in ArcMap 10.3, Python 2.7.8), that don't appear in the data frame.

The layers located in different disk drivers and in different folders and sub folders and each layer has different path orientation (right to left- different language from English). Only the MXD's located in "D:\desktop\Project" I using this code:

# -*- coding: cp1255 -*-
import arcpy,os
from arcpy import env

env.workspace = r"D:\desktop\Project"
for mxdname in arcpy.ListFiles('*.mxd'):
    print mxdname
    mxd = arcpy.mapping.MapDocument(os.path.join(env.workspace, mxdname))
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
        print str(lyr.getExtent())
        if lyr.getExtent() != None:
            if df.extent.disjoint(lyr.getExtent()):
                arcpy.mapping.RemoveLayer(df, lyr)
                print u'Removed '+unicode(lyr) + ' lyr'
del mxd


For clarity, i asked it in arcpy - Remove layers in sub folders using python- path orientation - Geographic Information Systems Stack Exchange