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?
lyr.setSelectionSet ("NEW", set())
, but you still have to loop through every layer.