ERROR 000871 or Schema Lock Trying to write over shapefiles in iteration. UPDATE

07-10-2021 10:33 AM
New Contributor


Forgive me, my language is probably off - I'm very new to programming and this is the first real script I've been asked to write. It's simple, runs through some geoprocessing tools and then prints out a map. When I run it outside of a loop it works fine, however when I get to the point in the second iteration where a mask is extracted from an RGB image file it fails, this is the error I get:

File "C:/", line 34, in <module>
RuntimeError: ERROR 000871: Output Raster: Unable to delete the output C:\Extract_RSF.

I've also gotten a schema lock error, my worry is that the shapefiles I'm looking to overwrite are locked for some reason. I run this script through PyCharm - ArcGIS Pro is closed so for the life of me I cannot figure out why it locks or why it cannot be overwritten. I believe it will do the same thing with the other 2 shape-files required in the process that need to be overwritten in every iteration.

Here's the code:





import arcpy
from arcpy import env
env.workspace = r"C:\Users\nahgl\Firewatch\Firewatch.gdb"
env.overwriteOutput = True

fc = "Parcels"
field = ["APN_8"]
RGB_of_RSF = arcpy.Raster("RSF_RGBe_FW2020")
Parcels = "Parcels"
Buildings = "Buildings"
SQL_Expression = "APN_8 = '26603028'"
Map_Expression = "OBJECTID = 1"
Symbology50 = r"C:\Users\nahgl\Firewatch\Python\Symbology_50.lyrx"
Symbology100 = r"C:\Users\nahgl\Firewatch\Python\Symbology_100.lyrx"

with arcpy.da.SearchCursor(fc, field) as cursor:
    for row in cursor:
        APN = "APN_8 = " + "'" + row[0] + "'"

        # Process: Select Parcel (Select) (analysis)
        Selected_Parcel = r"C:\Users\nahgl\Firewatch\Firewatch.gdb\Parcels_Select"
        arcpy.analysis.Select(in_features=Parcels, out_feature_class=Selected_Parcel, where_clause=APN)

        # Process: Select Building (Select) (analysis)
        Selected_Buildings = r"C:\Users\nahgl\Firewatch\Firewatch.gdb\Buildings_Select"
        arcpy.analysis.Select(in_features=Buildings, out_feature_class=Selected_Buildings, where_clause=APN)

        # Process: Extract by Mask (Extract by Mask) (sa)
        APN8_Raster = r"memory\Extract_RSF"
        Extract_by_Mask = APN8_Raster
        APN8_Raster =, in_mask_data=Selected_Parcel)

        # Process: Buffer 100 (Buffer) (analysis)
        Buffer_Output_100 = r"memory\Buffer_100"
        arcpy.analysis.Buffer(in_features=Selected_Buildings, out_feature_class=Buffer_Output_100,
                              buffer_distance_or_field="100 Feet", line_side="FULL", line_end_type="ROUND",
                              dissolve_option="ALL", dissolve_field=[], method="PLANAR")

        # Process: 100ft Buffer Symbology (Apply Symbology From Layer) (management)
        Buffer_Output_100 =,

        # Process: Buffer 50 (Buffer) (analysis)
        Buffer_Output_50 = r"memory\Buffer_50"
        arcpy.analysis.Buffer(in_features=Selected_Buildings, out_feature_class=Buffer_Output_50,
                              buffer_distance_or_field="50 Feet", line_side="FULL", line_end_type="ROUND",
                              dissolve_option="ALL", dissolve_field=[], method="PLANAR")

        # Process: 50ft Buffer Symbology (Apply Symbology From Layer) (management)
        Buffer_Output_50_Final =,

        # Process: Selecting the Project file = Firewatch & Map = RSF_Parcels
        project ="C:\Users\nahgl\Firewatch\Firewatch.aprx")
        map = project.listMaps('RSF_Parcels')[0]

        # Process: Selecting Buffer_100 as Zoom in layer (Or by Parcels)
        layer = map.listLayers('Buffer_100')[0]

        # layer = map.listLayers('Parcel')[0] ##If Parcel is used as zoom in layer Map_Expression must be changed to SQL_Expression
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", Map_Expression)

        # Process: Selecting Layout that will be used
        lyt = project.listLayouts("RSF_Layout")[0]

        # Process: Listing all elements within the layout
        mf = lyt.listElements('MAPFRAME_ELEMENT', '*')[0]

        # Process: Setting the extent of the map camera to the selected layer (Buffer_100 or Parcel) & then zooming into frame, True, True))

        # Clear the 100 Buffer Selection so it isn't blue, "CLEAR_SELECTION")

        # Process: Importing the layout with the zoomed in image to a JPEG (Needs better naming)
            r"C:\Users\nahgl\Firewatch\scratch\RGB_%s" % APN[9:-1])






I've tried deleting the shapefiles after the image is created,  this is where I get the schema lock:

"arcgisscripting.ExecuteError: ERROR 000464: Cannot get exclusive schema lock. Either being edited or in use by another application or service.
Failed to execute (Delete)."

I've done a lot of Google sloothing which leads be to believe this could be a bug? Not sure, using Python 3.7, any advice or help would be much appreciated. Thank you.

0 Kudos
7 Replies
MVP Esteemed Contributor

you need to put your files into a folder... writing to, and reading from the root folder (C:\) is just asking for trouble.

Also, overwriting files is controlled at the project level in Pro, under Geoprocessing Options or by setting the flag within the script (arcpy.env.overwriteOutputs=True) as you have done.  However, if a file is used within a loop it may not be able to be deleted until the whole process is complete.




a gdb in a gdb? 

... sort of retired...
0 Kudos
New Contributor

I removed the middle file paths, sorry for complicating things. Paths are like this:

r"C:\Users\nahgl\OneDrive\Desktop\GIS Project\Firewatch\Firewatch.gdb\Parcels_Select"
Would a pause work before it iterates again to clear the lock?
0 Kudos
MVP Frequent Contributor

To be sure of no unintended stuff going on with your paths I'd make sure everything is named with the same raw formatting r'...' as per

project ="C:\Fire.aprx")

I think you could avoid a lot of the overrides by using the memory workspace and deleting the memory objects each loop.

I'd follow Dan's suggestions in the first instance.

A final irrelevant point, I've not seen % used in strings very often, I'd say f strings or .format() is preferred.

0 Kudos
New Contributor

I was reading up on memory workspaces but not quite getting how to implement it. The extracted mask, buffer_100, and buffer_50 would only exist in memory and then it would erase it after the JPEG is produced?

0 Kudos
MVP Esteemed Contributor

I would try it at least.  It is better than trying to delete things until outputs are derived

... sort of retired...
0 Kudos
New Contributor

What about using the scratch workspace?

0 Kudos
MVP Esteemed Contributor

Did you try it? Nothing is going to happen to the inputs data if you try stuff in the scratch workspace or in_memory.  Either or.. Focus on getting to work, then optimize for speed or convenience after

... sort of retired...
0 Kudos