0

I have a python addin tool that looks for layers in the TOC using ArcMap. I need to be able to replace these layers with new layers of the same name in the same workspace. I'm working with shapefiles. The lock file is not releasing when I remove the layer. I use arcpy.mapping.RemoveLayer(), then with the os module, go into workspace where the layer was located and use os.remove() to delete the files. Unfortunately this keeps failing due to the schema lock. Is there a way for ArcMap to release the lock. The lock is released if I right-click the shapefile and remove it from the TOC, but I need to wrap this functionality within the addin tool using python.

layerPath = layer.workspacePath 
layerName = layer.name
arcpy.mapping.RemoveLayer(df, layer)
arcpy.RefreshActiveView()
for file in os.listdir(layerPath):
    if file.startswith(layerName):
        os.remove(os.path.join(layerPath, file))

How do I remove the schemalock?

  • what version of arcgis desktop are you using – NULL.Dude Apr 12 '18 at 18:29
  • I wouldn't think this would be necessary, but it may be worth trying to delete the reference to the layer object: del layer. – crmackey Apr 13 '18 at 3:16
  • I'm using 10.5. I have also tried mimicking the same code through the python window and have the same result. The lock file remains. – KWoosley Apr 16 '18 at 12:27
  • Also, I tried to del layer. This removes the object in code only. The lock file is not phased. – KWoosley Apr 16 '18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.