Cannot delete temp gdb due to lock from executing program

1786
12
Jump to solution
06-03-2022 02:30 PM
SFM_TravisBott
Occasional Contributor III

I am having trouble with the last phase of a script for a script tool: I cannot figure out how to delete my temporary gdb to close out the script. Having the program that's executing the script open retains a lock in the gdb...for instance when I close Jupyter (or VS code, or IDLE) I can see the lock file in the gdb disappear. How can I construct my code so this isn't an issue when made into a script tool?

There have a been a few other posts on this topic and none of those solutions seem to be quite right. The general advice has been to add an arcpy.env.overwriteOuput = True, and that has not been effective.

The code that creates the gdb:

#Create temporary Workspace
arcpy.AddMessage("\nCreating temporary workspace")
tempName = "incorp" + outVersion + "_TEMP"
tempGDB = os.path.join(outFolder, tempName + ".gdb")
arcpy.management.CreateFileGDB(outFolder, tempName)
arcpy.env.workspace = tempGDB
arcpy.env.overwriteOutput = True

The script then goes on and does several operations within that temp gdb, clipping, erasing, querying, adding fields, etc. Then when I go to delete it...

#Delete intermediate data
arcpy.AddMessage("\nDeleting intermediate data")
todel = (tempGDB, union, lastIncorp_xml)
for dataset in todel:
if arcpy.Exists(dataset):
arcpy.management.Delete(dataset)

I get ERROR 000601 - Cannot delete [path].gdb. May be locked by another application. Failed to execute (Delete).

Any thoughts on what can be done so the tool can clean up after itself?

 

0 Kudos
12 Replies
SFM_TravisBott
Occasional Contributor III

It's not explorer; I've isolated that one. 

I will reconfigure to work in memory and see how that goes. I built this following the model of another script that creates a temporary workspace and as far as I can tell I've followed it fine but maybe there's a detail I'm missing. 

0 Kudos
DarrenSmith
New Contributor III

I've just also gone through this same painful experience. I had my search cursor inside a with, tried deleting all possible variables that might have had a reference to the to temporary file geodatabase (fgdb). This didn't work and using the memory workspace wasn't an option since i was using some gp tools that don't support this. The solution: to individually delete each tmp feature class I'd created in the tmp fgdb before deleting the tmp fgdb itself.

BlakeTerhune
MVP Regular Contributor

Thank you for contributing to this discussion, @DarrenSmith. Your solution of deleting the objects in the fgdb before trying to delete the fgdb worked for me when I encountered this issue. I used Delete for both the objects and the fgdb. In my case, Compact was not necessary.

For reference, I was getting this error when trying to Delete the fgdb:

arcgisscripting.ExecuteError: ERROR 000601: Cannot delete

And I was getting this error when the Python TemporaryDirectory() was trying to clean up:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

...which caused the following:

NotADirectoryError: [WinError 267] The directory name is invalid