Remove lock on feature class

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....

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


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.