jplay

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

Discussion created by jplay on Apr 21, 2014
Latest reply on Apr 29, 2016 by pcfbobbymale77
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?

Outcomes