Select to view content in your preferred language

Cannot delete... May be locked by another application.

18688
15
04-21-2014 04:52 PM
JohnLay
Frequent Contributor
I've written a long script that selects a bunch of different feature classes and exports them to a file geodatabase by an attribute. Then I export those feature classes to shapefiles. Next, I zip the geodatabase into its own file and all the shapes into their own file. Finally, I delete the geodatabase and shapes so that all I'm left with is the 2 zip files (We do this for a FEMA product) in the output folder.

No problems.

Recently I learned that I needed to include several tables in the database as well, so I inserted some code that searches one table for an attribute and writes the output to a list. Then I use that list to query ANOTHER table to create a table in the database.

The problem is, now with this new bit of code, the geodatabase will not delete and I receive ExecuteError: ERROR 000601: Cannot delete C:\~Working\FRIS\Test2\Alamance\NCFlood_Prelim_Alamance_GDB.gdb. May be locked by another application. Failed to execute (Delete).

New bit of code:
# L_COMM_INFO
CIDList = []
S_POL_AR = os.path.join(FRIS_FGDB, "S_POL_AR")
S_POL_ARTable = arcpy.SearchCursor(S_POL_AR)
for ID in S_POL_ARTable:
    FIELD = "CID"
    Val = ID.getValue(FIELD)
    CIDList.append(Val)
NewCIDList = list(set(CIDList))

L_COMMUNITY_INFO = "Database Connections\NC_FLOOD.sde\NC_FLOOD.DBO.L_COMMUNITY_INFO"
L_COMMUNITY_INFOView = os.path.join(FRIS_FGDB, "L_COMM_INFOView")
L_COMMUNITY_INFOTable = os.path.join(FRIS_FGDB, "L_COMM_INFO")
arcpy.TableToTable_conversion (L_COMMUNITY_INFO, FRIS_FGDB, "L_COMM_INFO")
arcpy.TruncateTable_management (L_COMMUNITY_INFOTable)
arcpy.MakeTableView_management (L_COMMUNITY_INFO, L_COMMUNITY_INFOView)
for ITEM in NewCIDList:
    CLAUSE = ("{0} = '{1}'".format ("CID", ITEM))
    arcpy.SelectLayerByAttribute_management (L_COMMUNITY_INFOView, "NEW_SELECTION", CLAUSE)
    arcpy.Append_management (L_COMMUNITY_INFOView, L_COMMUNITY_INFOTable)
arcpy.Delete_management (L_COMMUNITY_INFOView, "")
del L_COMMUNITY_INFOTable


The delete code looks like this:
# Delete Files
Delete_Shapes = []
for shapefile in os.listdir(OUTPATH):
    if fnmatch.fnmatch(shapefile, '*.shp'):
        Delete_Shapes.append(shapefile)
    elif fnmatch.fnmatch(shapefile, '*.gdb'):
        Delete_Shapes.append(shapefile)
    else:
        pass

del shapefile

for shapefile in Delete_Shapes:
    get_file = os.path.join(OUTPATH, shapefile)
    arcpy.AddMessage("Deleting " + shapefile)
    arcpy.Delete_management(get_file)


The error occurs every time with the new code and never without.

When I use Unlocker.exe to unlock and delete the folder so that I can start all over again, I am told that RuntimeLocalServer.exe is the process locking the database.

What is wrong with the new bit of code that causes the RuntimeLocalServer lock? And how do I fix it?
Tags (2)
0 Kudos
15 Replies
NeilAyres
MVP Alum
Are you deleting the cursor somewhere...
S_POL_ARTable = arcpy.SearchCursor(S_POL_AR)

Something like this :
if S_POL_ARTable:
    del S_POL_ARTable

Good luck,
Neil
0 Kudos
JohnLay
Frequent Contributor
Yes, that was the first thing I tried. I haven't been able to find anything that will release the database programmatically. There are no .LOCK files in the database, yet arc seems to think that it is still in use.

I can't even delete the database in Catalog outside of the script. I have to exit completely first.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Edit: Looking at your error message, it says you are trying to delete the whole .gdb --- is that what you want to do? In any case, the code you posted has nothing to do with the error you are recieving and it is very difficult to determine your issue.  You may have to post the appropriate offending code.

    "Cannot delete C:\~Working\FRIS\Test2\Alamance\NCFlood_Prelim_Alamance_GDB.gdb."


Maybe try setting the overwriteOutput environment to True?

arcpy.env.overwriteOutput = True


http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000004s000000
0 Kudos
JohnLay
Frequent Contributor
Thanks James, I'll try env.overwrite.

In answer to your other question, yes, I'm trying to delete the entire database after it has been zipped. We zip the data so that they are easily deliverable to FEMA, engineers, public, etc. After they are zipped, I don't need the database or shapes anymore because the master statewide files are stored in SDE--the the script exports FIRM data by county.

I didn't think that bit of code should have anything to do with the delete error either. Which is precisely why I've been scratching head for the last two days. It's just weird.

Nonetheless, when i strip the script of that bit of code, the files zip and then get deleted without issue. With the code, the geodatabase is locked by RuntimeLocalServer.exe.
0 Kudos
JohnLay
Frequent Contributor
Yeah... arcpy.env.overwriteOutput = True didn't work either.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks James, I'll try env.overwrite.

In answer to your other question, yes, I'm trying to delete the entire database after it has been zipped. We zip the data so that they are easily deliverable to FEMA, engineers, public, etc. After they are zipped, I don't need the database or shapes anymore because the master statewide files are stored in SDE--the the script exports FIRM data by county.

I didn't think that bit of code should have anything to do with the delete error either. Which is precisely why I've been scratching head for the last two days. It's just weird.

Nonetheless, when i strip the script of that bit of code, the files zip and then get deleted without issue. With the code, the geodatabase is locked by RuntimeLocalServer.exe.


overwriteOutput wouldn't make any difference in this case (I was confused because the code you posted is attempting to delete Feature Classes, NOT deleting the .gdb).

So....

Where is the code for deleting the .gdb?  You never posted it.
0 Kudos
JohnLay
Frequent Contributor
the delete database is part of the same process. The bit of code that deletes makes a list (Delete_Shapes) of all the *.shp and *.gdb in the directory. Then, it runs through the list using a for loop and deletes each object.

# Delete Files
Delete_Shapes = []
for shapefile in os.listdir(OUTPATH):
    if fnmatch.fnmatch(shapefile, '*.shp'):
        Delete_Shapes.append(shapefile)
    elif fnmatch.fnmatch(shapefile, '*.gdb'):
        Delete_Shapes.append(shapefile)
    else:
        pass

del shapefile

for shapefile in Delete_Shapes:
    get_file = os.path.join(OUTPATH, shapefile)
    arcpy.AddMessage("Deleting " + shapefile)
    arcpy.Delete_management(get_file)


In the code above, OUTPATH is the root directory, not the database itself.
0 Kudos
JamesCrandall
MVP Frequent Contributor
I see what you mean -- are you dynamically creating this .gdb?  Try setting your ScratchWorkspace to the folder that contains the .gdb you want to delete.   This works for me:



ws = r'H:\Documents\ArcGIS'
arcpy.env.workspace = ws
arcpy.env.scratchWorkspace = ws

items = arcpy.ListFiles('*.gdb')
for item in items:
   if item == 'test.gdb':
       arcpy.Delete_management(item)
       print "deleted" + str(item)

0 Kudos
JohnLay
Frequent Contributor
That doesn't release the database either.

Argh! this is completely frustrating!

I guess ultimately it doesn't really matter. I've inserted a try statement before the delete code (which is the very last process) to bypass the error and complete the script. The zips are created, which is really all I need. I can manually delete the garbage. I just wish I knew what was causing the issue.

Like I said previously, leaving the S_POL_ARTable searchcursor business out COMPLETELY resolves the problem. I just I could figure out what it was about that section of code that causes the issue.
0 Kudos