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.
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?
See ArcGIS Pro - Modifying Layer Definition query via ArcPY , you will need to work with the Python CIM access—ArcPy | Documentation .
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.
Hi, I tried this method but did not clear the definition query for me...
Did this issue ever get resolved? I tried using the listDefinitionQueries, setting the 'isActive' field to False and then using then using the updateDefinitionQueries function. But that just does the same thing as lyr.definitionQuery = None. So if you are iterating through multiple definition queries there is no way to automate their removal.
@adamwade1 , the only solution that I found to work was to use the function provided by @RogerDunnGIS on 02-02-2023 08:37 AM (see above in this thread).
In my Python scripts, I use the following function to overwrite existing definition queries with new ones:
def setDefinitionQuery(layer, sqlFilter):
layer.updateDefinitionQueries(
[
{'name': 'Query 1', 'sql': sqlFilter, 'isActive': True}
]
)
this works! thank you
@RogerDunnGIS, Thanks for providing this function. I'm really new to python, so please correct me if I'm wrong. If I understand your function correctly, I need to provide two variables to the function: "layer", and "sqlFilter" that the function uses? How do I call/use your function? Thanks so much for your help.