I need to scroll through an mxd and remove any layers that have broken data sources.
I have found
brknList = arcpy.mapping.ListBrokenDataSources(inMXD)
and
for lyr in arcpy.mapping.ListLayers(mxd, "*",df):
arcpy.mapping.RemoveLayer(df, lyr)
both work well, but not together.
Any suggestions?
arcpy Layer objects have a isBroken property you can check if is true or not. If it is true, then have your script remove the layer, instead of all layers
try this:
for layer in arcpy.mapping.ListLayers(mxd, "*", df):
if layer.isBroken:
arcpy.mapping.RemoveLayer(df, layer)
It works GREAT !!!
Your few lines of code just saved me from several days of frustration.
Thanks for your prompt reply.
Ted
You are quite welcome. Just out of curiousity, why are you needing to delete so many layers from so many map?
We have 15 years of legacy mxds and most have data sources long gone.
This way, I can log the broken data sources, remove them and then allow the end users to replace the data as needed.
Otherwise, it takes a VERY LONG TIME to open the mxds w/ all the broken data.
Yea that would be quite a pain to do manually.
If you don't mind can you mark this question answered, will make it easier for people to find in the future if they have a similar issue.