Hi everyone,
I am newbie to arcpy scripting. I have written the following script based on referring to previous posts. This script remove definition query for a layer in a map.
I run this in the python console of ArcMap.
The query works fine when giving path of the aprx as CURRENT. It removes the existing definition query.
The same query also runs when the path of the aprx is given. But it doest remove definition query
aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\MyProject9_Test.aprx')
# Specify the path to your ArcGIS project
#aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\99_temp_work\MyProject9_Test.aprx')
## This script removes all definition query in Map layers in Apro
import arcpy
import os
def remove_definition_query(layer_name, my_map=None):
# Specify the path to your ArcGIS project
#aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\99_temp_work\MyProject9_Test.aprx')
# Iterate through maps in the project
for map in aprx.listMaps():
# Check if a specific map is provided
if my_map and map.name != my_map:
continue
print(f"Checking map: {map.name}") # Debugging line
# Iterate through layers in the map
for layer in map.listLayers():
print(f"Found layer: {layer.name}") # Debugging line
if layer.name == layer_name:
try:
# Remove the definition query by setting it to None
layer.definitionQuery = None
print(f"Removed definition query for layer: {layer_name} in map: {map.name}")
except Exception as e:
print(f"Error removing definition query for layer {layer_name} in map {map.name}: {e}")
remove_definition_query("LFS_Replenishment_2_Dissolve1", 'TestMap1')
Query Output:
Checking map: TestMap1
Found layer: LFS`1
Found layer: LFS_Replenishment_2_Dissolve1
Removed definition query for layer: LFS_Replenishment_2_Dissolve1 in map: TestMap1
Found layer: MapGeniePremiumITM
Can someone tell me why this is query is not working correctly when given the path. I appreciate your help on this.
matter. FYI: The path file is correct.
In Pro example 2, they save the aprx when they are done, give it a try
Layer—ArcGIS Pro | Documentation
Thank you for your time Dan,
I did tried saving to a new layer file. It saves but the saved the file is similar to that of input ones. It didn't remove definition query.
it wasnt the layer being saved, but the project