Sometimes when I'm testing an ArcPy script and it ends with exception, ArcMap is still holding locks to some temp files (I run my scripts through ArcMap).

I cannot run the script any more, because it cannot delete/overwrite the temp files and in the end I have to restart ArcMap to release the locks.

Is there a way to do in a more clever fashion?


4 Answers 4


I've just come up with the following construction:

def main():
    # my entire code

if __name__ == "__main__":
    except Exception, e:
        import gc
        import traceback

This way I don't loose any messages and so far all locks have been released properly.

  • 1
    Will this work if ArcMap crashes though. I wouldn't think it would be an Expection in that case
    – Nathan W
    Sep 10, 2013 at 12:57
  • @NathanW Well, if ArcMap itself crashes then there's nothing that really can be done in terms of catching the fault. And also if it crashes, it needs to be started again and all lock files are being deleted (or they persist but are not used). So in terms of at least this question, an ArcMap crash is not a problem or am I missing something?
    – Michal
    Sep 10, 2013 at 13:03
  • 1
    But in your question you say it crashes ArcMap and then you have to start ArcMap again. But if it crashes isn't it all ready dead.
    – Nathan W
    Sep 10, 2013 at 13:10
  • @NathanW I've edited my question. It's not ArcMap that crashes but the script.
    – Michal
    Sep 10, 2013 at 13:25
  • That makes more sense :)
    – Nathan W
    Sep 10, 2013 at 13:51

You can delete all variables set and cursor rows within a try/except block:


  # your code here

  # delete your variables here
  del myVariable1, myVariable2, myVariable4, row, rows
  • 2
    It works, but deleting all variables is quite troublesome...
    – Michal
    Sep 10, 2013 at 12:44

The easiest way might be to just open Windows Task Manager (Alt+Ctrl+Del) and stop the python.exe process after the script crashed. After this the locks should be gone. When you restart your script, python.exe will be restarted as well.


I have not tested this but a GeoNet link by its user john.m.dye that arrived in my mailbox today suggests a way to do this (at least with ArcGIS Pro, but worth investigating for ArcMap equivalency too):

For local file geodatabases, the compact geodatabase tool will remove any invalid locks on the geodatabase that might not have been properly removed when editing stopped. For Enterprise Geodatabases, use the disconnect user tool to kill any active connections users have to the database, which should also kill their locks. You can follow that up with accept connections to block users from reconnecting while you do your thing, call compact geodatabase to ensure there are no invalid locks, do what you need to do, then call accept connections again to allow users to reconnect.

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.