1

I am using ArcGIS Pro 2.9

Using ArcPy I have to initiate multiple queries every day on 60+ layers and all these queries are piling up in Definition query tab for all these layers. I am unable to delete the queries using

lyr.definitionQuery = ""

OR

lyr.definitionQuery = None

both just deactivate the queries.

Is there a way to reuse the existing definition query again, like if using query name, using ArcPy?

Update:

Even replacing query did not help, what I tried is as follow

Old_DT = lyr.definitionQuery
New_DT = lyr.definitionQuery.replace(Old_DT,DT)
lyr.definitionQuery = New_DT

Still New query is added in query definition list.

3
  • Have you explored using CIM to alter the definition query? Most fine tweaking of layers in ArcPro can be done with that.
    – Hornbydd
    Mar 11 at 9:33
  • 1
    Similar questions have been asked and answered on Esri Community/GeoNet. See ArcGIS Pro - Modifying Layer Definition query via ArcPY
    – bixb0012
    Mar 11 at 16:33
  • Yes, I saw above post and everything regarding CIM went over my head, hence a new post.
    – Abdul Rauf
    Mar 12 at 10:27

1 Answer 1

3

You can use CIM to manipulate multiple definition queries that a layer may have, below is some sample code to demonstrate this. In this example the layer already has a Query Definition called "SillyQuery" imposed upon it. As you have spotted simply editing it causes a new query to be inserted into the list. This seems to be default behaviour that one cannot override but as it appears to be the query added to the end of the list, this code simply removes it and overwrites the list, this is how it stops the ever growing list of query definitions.

import arcpy
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers('Cables')[0] # Example layer

# Return the layer's CIM definition
l_cim = l.getDefinition('V2')

# Get Feature Table object
fTab = l_cim.featureTable

# Get the Query Definition Name, this assumes one is active
expName = fTab.definitionExpressionName

# If SillyQuery is active then adjust the expression
if expName == "SillyQuery":
    fTab.definitionExpression = "OBJECTID = 2"
    # This causes an insert of a new entry into the list
    
# Remove new entry in list
aList = fTab.definitionFilterChoices
aList = aList[:-1]
fTab.definitionFilterChoices = aList

# Push the changes back to the layer object
l.setDefinition(l_cim)
3
  • This Works as intended. Have not worked with CIM before, will look into this further. for CIM i have two more questions a) can we loop through queries? b) as i said earlier "" or None is not deleting queries. Can we delete queries with CIM?
    – Abdul Rauf
    Mar 12 at 11:43
  • Also If the SillyQuery does not exists how can i create it?
    – Abdul Rauf
    Mar 12 at 11:46
  • 1
    In the code aList is the list of queries. You could insert/delete queries. I would recommend you get the CIM viewer installed it will help you understand the object model you need to navigate.
    – Hornbydd
    Mar 13 at 1:46

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.