Can't delete a file GDB in Python Script Tool

4270
5
07-08-2010 01:52 PM
NilsBabel
Occasional Contributor II
I'm trying to delete a file geodatabase in my script and I keep getting the dreaded may be locked by another application error.  It's not in use by any other application.  The script creates the gdb.  I've reset my environments, refreshed the catalog, and even tried deleting the gp first and then using os.remove to delete the gdb but no luck.  I have made sure to delete all my search and update cursors along the way so that shouldn't lock it.  Once the script completes(with the error) I can use ArcCatalog to delete the gdb with no problem.  If anyone has a suggestion I'm all ears.  At this point I've spent almost as much time developing the entire complex script as I have trying to delete the geodatabase.

Thanks,
Nils
0 Kudos
5 Replies
ChrisSnyder
Regular Contributor III
Locks often remain for the duration of an ArcMap or ArcCatalog session, even though you aren't currently looking/accessing the FGDB. Also, the gp object does the same thing! Make sure you don't have any ArcWhatever or python.exe running (that are looking or ever looked at one time at the FGDB), and then run your script.
0 Kudos
DeeleshMandloi
Esri Contributor
Nils,
Once the script completes(with the error) I can use ArcCatalog to delete the gdb with no problem.

  The fact that you can delete the file gdb using ArcCatalog on script completion suggests that the locks are still present when you try to delete the file gdb in script. Apart from cursors there are certain operations that can also create locks. One such case that I had come across was when using the "make feature layer" tool. If the tool is used to create a feature layer from a file gdb feature class, it creates a lock on the file gdb ( you can verify this by running the script that calls make feature layer in a debugger and look at the files that make up the file gdb. There will be few *.lock files present. Note that they will go away once the script completes). If this is the case, you should first call the Delete tool on the feature layer created using the make feature layer tool and then you should call delete tool to delete the file gdb.

Hope this helps
Deelesh
AnaSepulvedaN
New Contributor

This one solve my problem. Thank you

0 Kudos
SimonWillis
New Contributor II
Also do you release objects from memory at the end of your script? Just wondering if that could maintain a lock?
del gp, rows, fields, whatever
0 Kudos
RhettZufelt
MVP Frequent Contributor
I had this problem using python for metadata importing.  turns out I had to make my .py script call an os.system(batchfile.bat).  Inside this batch file, I ran a separate metadataimport.py.  this way, the metadataimport spawns a separate python.exe (while the other is still running), then upon completion, removes the lock files, as that was the only process under the new python.exe.

hope this helps,

R_

Professional ESRI beta tester

I'm trying to delete a file geodatabase in my script and I keep getting the dreaded may be locked by another application error.  It's not in use by any other application.  The script creates the gdb.  I've reset my environments, refreshed the catalog, and even tried deleting the gp first and then using os.remove to delete the gdb but no luck.  I have made sure to delete all my search and update cursors along the way so that shouldn't lock it.  Once the script completes(with the error) I can use ArcCatalog to delete the gdb with no problem.  If anyone has a suggestion I'm all ears.  At this point I've spent almost as much time developing the entire complex script as I have trying to delete the geodatabase.

Thanks,
Nils
0 Kudos