8

I'm trying to find a way to determine whether a feature class is locked before trying to alter it.

In one database I want to remove a feature class completely, and in another I want to add extra fields. I can't do either if they're locked.

I was expecting to find something in arcpy.Describe() but haven't found anything. Something like

if arcpy.Describe(myFC).isLocked:
    print "Can't proceed - feature class is locked"
else:
    arcpy.Delete_management(myFC)

Is there a way to check feature class locks using arcpy?

2 Answers 2

12

Found it - I was using incorrect terminology in my searches.

There is a tool called Test Schema Lock which will return True or False based on whether a lock can be applied or not. So my code would be:

if not arcpy.TestSchemaLock(myFC):
    print "Can't proceed - feature class is locked"
else:
    arcpy.Delete_management(myFC)

Because it returns True if the feature class isn't locked (i.e. a lock can be applied), an if not needs to be used to find where it returns False (it is locked).

2
  • 1
    Does this work on the entire gdb as well as individual feature classes? What about shapefiles?
    – Craig T
    Commented Apr 19, 2017 at 13:57
  • 1
    @CraigT it's only 4 lines, have you tried it? I suspect it will work for shapefiles as well, but I haven't tried. I don't know about entire gdb - locks are usually per feature class so I'm not sure if a gdb reports it is locked. You could loop through each feature class to determine which is locked and which isn't
    – Midavalo
    Commented Apr 19, 2017 at 14:05
1

This syntax is outdated now.

You can use arcpy.testSchemaLock() instead.

Link to Doc. https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/testschemalock.htm

1
  • What syntax is outdated? Your suggested solution is the same as the accepted answer
    – Midavalo
    Commented Sep 8, 2023 at 19:53

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.