11

When i try to delete a feature class , after or during the use of an Arcgis engine app which uses the same feature class i got an error concerning the lock , so i can't get ride of this lock until i restart the computer.

i would like to know a way to remove locks using arcpy or arcobjects 10.1 , it would be perfect if i can get ride of the lock inside the arcengine app which also locks the geodatabase , i have seen different posts in SE about locks but no one of them gives the solution to free locks using ArcObjects.

1
  • 1
    I had a similar question a while back (gis.stackexchange.com/q/28977/8104). I was unable to find an answer about the locks, although there was good information about deleting in memory objects.
    – Aaron
    Commented Aug 25, 2012 at 12:41

4 Answers 4

12

If you are using Arcpy scripting, data lock issues have become even more problematic with 10.1.

If the locks are being created by your code, then you aren't cleaning up after yourself. Release all references to feature classes, workspaces, cursors, etc. The locks are removed when you have no more references to the objects being locked.

Well, that is the way it should work, but it rarely does. Usually a code will work occasionally, but often crash inexplicably in different places.

The problem appears to be that locks are just left sitting there `for a while', but the code runs much faster than the speed at which the locks are removed. However, some of the inbuilt Arc tools seem to force locks to be cleared on demand (as locks apply to entire GDBs all at once, a lock will prevent you from working with any of the contained Feature Classes). These tools are arcpy.Compact_management() and arcpy.Exists().

Here is a little function that I use within my code that has dramatically increased reliability (for a script that creates and edits multiple GDBs and Feature Classes within them):

def clearWSLocks(inputWS):
  '''Attempts to clear locks on a workspace, returns stupid message.'''
  if all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
    return 'Workspace (%s) clear to continue...' % inputWS
  else:
    return '!!!!!!!! ERROR WITH WORKSPACE %s !!!!!!!!' % inputWS

It is used by simply passing the workspace (GDB) path to the function, and should be done after every operation on either the workspace (i.e. GDB creation) or Feature Classes within the workspace (i.e. Cursors, adding fields, calculations, etc.). For example (shown here as a standalone script, with the function at the top; to use the function, copy it and paste it between the imports and the actual program, as shown here):

import arcpy

def clearWSLocks(inputWS):
  '''Attempts to clear locks on a workspace, returns stupid message.'''
  if all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
    return 'Workspace (%s) clear to continue...' % inputWS
  else:
    return '!!!!!!!! ERROR WITH WORKSPACE %s !!!!!!!!' % inputWS

GDBpath = 'C:/Temp/'
GDBname = 'Test.gdb'
tableName = 'SweetFC'
arcpy.CreateFileGDB_management(GDBpath, GDBname)
print(clearWSLocks(GDBpath+GDBname))
arcpy.CreateTable_management(GDBpath+GDBname, tableName)
print(clearWSLocks(GDBpath+GDBname))
# etc....
3
  • 3
    Compact does not work if there is a lock. It will indeed increase performance when you run a lot of operations of a FGDB, but at the cost of its own runtime. Commented Sep 20, 2012 at 0:06
  • just wondering why there is two times Exists, I mean the third statement in if all() in clearWSLocks?
    – makak
    Commented Oct 24, 2016 at 5:56
  • I can concur, I have not found this to clear locks. :( and I have tried many different things so far.
    – Vidar
    Commented Jan 20, 2017 at 11:52
8

A file geodatabase has 3 types of locks.

  1. SR - schema lock
  2. RD - read lock
  3. ED - edit lock

The proposed solution by @StacyR will work in all situations except for exclusive edit locks (ED) according to arcgis help documentation.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//003n0000007t000000

0
7

If the locks are being created by your code, then you aren't cleaning up after yourself. Release all references to feature classes, workspaces, cursors, etc. The locks are removed when you have no more references to the objects being locked.

1
  • 1
    I know this thread is pretty old, but I'm having an issue with being able to delete a FGDB within arcpy. What exactly do you mean by "Release all references to feature classes, workspaces, cursors, etc." How would I go about releasing the said references?
    – GeoJohn
    Commented Oct 23, 2014 at 19:55
0

I had problems with locks created by arcpy.da.UpdateCursor:

...
...
with arcpy.da.UpdateCursor(input, fields) as cursor:
        for row in cursor:
            ...
            ... 
            cursor.updateRow(row) 

Leftover lock after the update finished:

Parcel_Polygon.shp.--.8640.13212.sr.lock

I had to add after each UpdateCursor

del cursor
arcpy.ClearWorkspaceCache_management()

After the addition the "leftover" lock created by the update was removed. I assume that this is a version specific error. Most likely it will not apply universaly accros all arcpy versions as I hope ESRI will address this anomaly soon. Nevertheless it might be good practice to release the variables/objects and clear the caches whenever possible.

When the the Python interpreter was closed it also released the lock.

11/2023 ArcGIS Pro 3.1

>>> print(sys.version)
3.9.16 [MSC v.1931 64 bit (AMD64)]
>>> version = arcpy.GetInstallInfo()['Version']
>>> print(version)
3.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.