0

I would like to know the Python code to clear ALL selected features. Just like the "Clear Selected Features" button works. I found this Q&A from 8 years ago:

How to Clear Selected Features in Arcmap using Python

This is the code given:

arcpy.SelectLayerByAttribute_management(Table, "CLEAR_SELECTION")

However, in the given code, you have to stipulate a table. I don't want to write code for every table/feature class. I would prefer code to clear ALL selected features. Is this possible?

Simply removing the table parameter does not work.

Edit:

Found this, which seems to be on the right track: https://community.esri.com/thread/108712

However, when I run the below code, it executes, but nothing happens:

>>> import arcpy
... mxd = arcpy.mapping.MapDocument(r"FILEPATHHERE")
... df = arcpy.mapping.ListDataFrames(mxd, "Main")
... for lyr in arcpy.mapping.ListLayers(mxd,"",df):
...     print (lyr.name)+" ...is going to be cleared now. Running 'arcpy.SelectLayerByAttribute_management'"
...     arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
...     print (lyr.name)+" ...should be cleared now."
...     
Buildings ...is going to be cleared now. Running 'arcpy.SelectLayerByAttribute_management'
Buildings ...should be cleared now.
Site ...is going to be cleared now. Running 'arcpy.SelectLayerByAttribute_management'
Site ...should be cleared now.
>>>

I ran the same thing with user2856’s suggestion:

>>> import arcpy
... mxd = arcpy.mapping.MapDocument(r"FILEPATHHERE")
... df = arcpy.mapping.ListDataFrames(mxd, "Main")
... for lyr in arcpy.mapping.ListLayers(mxd,"",df):
...     print (lyr.name)+" ...is going to be cleared now. Running...'"
...     lyr.setSelectionSet("NEW",set())
...     print (lyr.name)+" ...should be cleared now."
...     
Buildings ...is going to be cleared now. Running...'
Buildings ...should be cleared now.
Site ...is going to be cleared now. Running...'
Site ...should be cleared now.
>>>

Of course, I do not actually have “FILEPATHHERE” in my code; the correct, actual file path is there (just to be clear).

All selected features remain selected when executing the above code. If I run “arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")” separately and individually on each layer, it works correctly and the selected features are deselected. What’s the problem?

1
  • 1
    You can also use lyr.setSelectionSet ("NEW", set()), but you still have to loop through every layer.
    – user2856
    Aug 4, 2020 at 0:24

1 Answer 1

1
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\thisIsAnExamplePath\exampleMap.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
    arcpy.SelectLayerByAttribute_management(lyr.name,"CLEAR_SELECTION")

Figured it out. Needed to pass the data frame as data frame object with [0], then use the ".name" to pass the layer name as a string into the parameter.

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.