How do I remove layers with broken data sources?

3798
5
09-05-2014 08:54 AM
TedPrescott
New Contributor III

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?

Tags (1)
0 Kudos
5 Replies
IanMurray
Frequent Contributor

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)

TedPrescott
New Contributor III

It works GREAT !!!

Your few lines of code just saved me from several days of frustration.

Thanks for your prompt reply.

Ted

0 Kudos
IanMurray
Frequent Contributor

You are quite welcome.  Just out of curiousity, why are you needing to delete so many layers from so many map?

0 Kudos
TedPrescott
New Contributor III

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.

0 Kudos
IanMurray
Frequent Contributor

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.

0 Kudos