Trouble Removing Schema Lock After arcpy.MakeFeatureLayer (Pro Only)

14001
14
06-13-2017 06:31 AM
ScottDavis
Frequent Contributor

I'm having trouble removing a schema lock from a feature class in a file geodatabase after running arcpy.MakeFeatureLayer(). The typical arcpy.Delete(layer) isn't working.

I only experience this issue when running this with Pro's (1.4.1) version of python. It works fine with ArcGIS Desktop. Any ideas?

Demo

Here's a script that demonstrates the issue:

import arcpy
arcpy.management.CreateFileGDB(r"C:\temp", "test.gdb", "CURRENT")
arcpy.management.CreateFeatureclass(r"C:\temp\test.gdb", "Test", "POLYGON", None, None, "ENABLED", "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]];-20037700 -30241100 10000;#;#;0.001;#;#;IsHighPrecision", None, 0, 0, 0)
arcpy.env.workspace = "c:\\temp\\test.gdb"
layer = arcpy.MakeFeatureLayer_management('Test', 'NewTest')
arcpy.Delete_management('NewTest', 'Layer')
del layer
arcpy.env.workspace = ""
#: lock still exists
arcpy.Delete_management("C:\\temp\\test.gdb") 

"""
This throws an exception: 
arcgisscripting.ExecuteError: ERROR 000601: Cannot delete C:\Temp\test.gdb.  
May be locked by another application.
"""
Tags (2)
14 Replies
TomaszTARCHALSKI1
Esri Contributor

Hi, at version ArcGIS Pro 2.4.2, I have very, if not the same, behavior: I cannot delete layer, because of  ERROR 000464: Cannot get exclusive schema lock.

Workflow of Script:

  • my script is accessing one  feature class in fgdb stored on NAS --> meaning read only files
  • next it create layers (in local fgdb ), do some intersection within my zone (AOI) and export to local feature class
  • when trying to delete the layer, the scripts fails due to the above error.

Here are my observation:

  • Executing script standalone and using python 3x and based on fgdb stored on NAS (read only files) --> it fails 
  • Executing script in Pyton 2.7 --> it works!
  • Executing script through toolbox in ArcGIS Pro --> it works!
  • Executing script standalone and using python 3.x and based on fgdb stored locally (so no attributes of read only) --> it works!

I am attaching zip file with script, and 2 fgdb:

  1. one fgdb which is created normally (access  read/write)
  2. the second fgdb where after creation, I've changed read/write rights within windows explorer and properties of folder.

As said above if executing script with "read only" fgdb by:

  • "c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3" test.py - it fails
  • "C:\Python27\ArcGISx6410.6\python.exe" test.py -it works

As said above if executing script with "read/write" fgdb by:

  • "c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3" test.py - it works
  • "C:\Python27\ArcGISx6410.6\python.exe" test.py -it work

link to dropox with zip file of script and data

What is the problem? Anyone ?

Thank you, Tomasz

screenshot of script

ThomasL
Regular Contributor

Thank you Tomasz TARCHALSKI‌ for documenting this bug.

I am running version ArcGIS Pro 2.4.3 and have the same issue, but on a ShapeFile.

My workaround is to duplicate the data set to avoid this, not very ideal. 

Any update to his Shaun Walbridge‌ ?

0 Kudos
RobertHolliday
Occasional Contributor

Working with tech support BUG-000129022 was logged. In my case, updating FC attributes in a scratch FGDB with update cursors caused locks which prevented deletion of the scratch FGDB. Substituting calculate field for the update cursors is a viable option in this case as the FC in question only contains a few thousand features.

Specifically: arcgisscripting.ExecuteError: ERROR 000601: Cannot delete C:\TEMP\ESRI_Case_geodatabase_locks\proc.gdb. May be locked by another application.
Failed to execute (Delete).

In theory creating the cursors using the following construct should release locks after the cursor executes.

 with arcpy.da.UpdateCursor(fc, [columns], where clause) as cursor:

     for row in cursor:

        do some calculation

        cursor.updateRow(row)

0 Kudos
DarrenConly
Regular Contributor

Hi All,

I'm still having this challenge. To summarize the code below:

  1. From a point feature class I'm making a point feature layer (if the point feature layer already exists I specify to delete and overwrite it)--see line 7 in code sample
  2. Do stuff with the point feature layer (lines 9-55)
  3. Once done doing stuff with the point feature layer, I specify to try and delete it (line 59). But I cannot delete it because it has a schema lock on it.

More info:

  • The issue does not affect when I run in a desktop environment. It only comes up once I publish the tool using the code to my agency's Portal hosted on ArcServer
  • This creates problems because there are various points in my script where the same process is re-run on the same feature class, so the feature layers for
  • The problem is intermittent (but happens more than half the time I run the tool), and I can't pin down the exact conditions that trigger it.
  • I've tried the following with no success:
    • Putting feature layer in "in-memory"
    • Making copy of source feature class in scratch GDB
    • adding a time.clock() suffix to the feature class name so that every time it is created it has a unique name.

What I want to know:

Is there anything I can do to "release" the feature layer so that it can be deleted?

I hope this is the right thread to post this on. Any help much appreciated.

def intersection_density(fc_project, fc_intersxns, project_type):
    arcpy.AddMessage("Calculating intersection density...")
    
    fl_project = 'fl_project' #weirdly, the project line feature class has no problems

    # the intersections fl is what's problematic. Trying to create in temp memory workspace
    fl_intersxns = os.path.join('memory','fl_intersxns')

    extest = arcpy.Exists(fl_intersxns)
    arcpy.AddMessage("Does intersection fl exist? {}".format(extest))

    try:
        
        if arcpy.Exists(fl_project): arcpy.Delete_management(fl_project)
        arcpy.MakeFeatureLayer_management(fc_project, fl_project)
        
        # the first time through, it seems fine and creates the intersections feature layer without problems
        if arcpy.Exists(fl_intersxns): arcpy.Delete_management(fl_intersxns)    
        arcpy.MakeFeatureLayer_management(fc_intersxns, fl_intersxns)


        if project_type == params.ptype_area_agg:
            fc_buff = fc_project
        else:
            params.intersxn_dens_buff
            fc_buff = r"memory\temp_buff_qmi"
            arcpy.Buffer_analysis(fl_project, fc_buff, params.intersxn_dens_buff)

        fl_buff = "fl_buff"
    
        if arcpy.Exists(fl_buff): arcpy.Delete_management(fl_buff)
        arcpy.MakeFeatureLayer_management(fc_buff, fl_buff)

        buff_acres = get_poly_area(fl_buff)

        arcpy.SelectLayerByLocation_management(fl_intersxns, "INTERSECT", fl_buff, 0, "NEW_SELECTION")

        intsxn_34 = 0
        col_link_cnt = "LINKS"

        with arcpy.da.SearchCursor(fl_intersxns, [col_link_cnt]) as cur:
            for row in cur:
                if row[0] > 2:
                    intsxn_34 += 1

        intersxns_per_acre = intsxn_34 / buff_acres if buff_acres > 0 else 0
        arcpy.AddMessage("Successfully calculated intersections per acre.")

        return {"Intersxn_34_per_acre": intersxns_per_acre}
        

    except:
        msg = "{}, {}".format(arcpy.GetMessages(2), trace())
        arcpy.AddWarning(msg)
    finally:
        if arcpy.Exists(fl_intersxns): 
            try:
                #HERE IS WHERE IT THROWS ERROR 000464: cannot delete because of schema lock. Why is it still locked?
                arcpy.Delete_management(fl_intersxns)
            except:
                msg = "trying arcpy.Delete_management: {}, {}".format(arcpy.GetMessages(2), trace())
                arcpy.AddWarning(msg)

        n = gc.collect()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
HanZheng
Emerging Contributor

My situation is that my python notebook script in my ArcGIS Pro project needs to read a *.shp as input and drop bunch of files in my default geodatabase. After the script finishes, there is always a lock to that input shapefile unless I closed the ArcGIS project. Ideally, I'd like to have that lock release after Python script finishes running.

I am looking for a solution (maybe a line of python script in the end) that the scheme lock of that input shapefile can be released after script completes execution without me closing ArcGIS Pro project.

0 Kudos