Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
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.
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.
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....
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.
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.
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?
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