Delete vs deactivate layer definition query

1111
3
04-17-2020 01:11 PM
JeffThomasILM
Occasional Contributor II

I'm trying to automate clearing an existing layer definition query and setting a new one. The code I wrote does successfully set the new def query, but it deactivates the old query rather than actually deleting it. The Layer class code sample 2 shows how it presumably would be done, but it doesn't actually delete it. ArcGIS Pro - Modifying Layer Definition query via ArcPY also essentially asks the same question, but it was never fully answered.

for map in aprx.listMaps():
    lyr = map.listLayers("Zoning History")[0]
    lyr.definitionQuery = None  ## have also tried ""
    lyr.definitionQuery = "CaseNumber = '" + case + "'"‍‍‍‍‍‍‍‍

I've tried setting it as None and "" - both result in deactivating rather than deleting the old query. It's not a showstopper, but it's kind of weird. If I change the def query again, it will overwrite the old-new query, but the old-old query (which was not set using ArcPy, if it matters) still remains present but inactive.

I started thinking about other scenarios, such as what if I wanted to deactivate rather than delete a query? I haven't found any documentation for how to do that properly (rather than unintentionally) with ArcPy.

Tags (1)
3 Replies
JeffThomasILM
Occasional Contributor II

Update: there's clearly something buggy with the original query on my map layers. After I manually deleted all the queries through the UI, ArcPy worked correctly when I added a new query, then changed the query to a new query, leaving no previous query as inactive. The new question is whether it's just me or if it's a bug that any layer with an existing query set through the UI has the same issue when attempting update with ArcPy. And my other question: what if I wanted to deactivate the def query with ArcPy?

JoshuaBixby
MVP Esteemed Contributor
LindsayRaabe_FPCWA
Occasional Contributor III

I've just tested the below line in our python script that looks at a set map (Data Editing) in the Current project, and had the last 2 lines repeated for all the different layer names that I wanted it to update. Successfully deleted the definition query from each layer. 

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Data Editing")[0]

for lyr in m.listLayers("Layer 1"):    ###Repeat these 2 lines for each layer
    lyr.definitionQuery = None

for lyr in m.listLayers("Layer 2"):    
    lyr.definitionQuery = None

 

I plan to write a simple script that can be used on any project and map which will just go through all layers in the current map and wipe and definition queries. 

 

Lindsay Raabe
Forest Products Commission WA
0 Kudos