Select to view content in your preferred language

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

18689
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
NaimeCelik
Regular Contributor

Did anyone find a solution for how to delete locked shp files, I have same problem.. and very very frustrating...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It seems you are using the older/original search cursor instead of the newer data access (da) search cursor.  Have you tried using the da.SearchCursor with a Python with statement?  Does that change the result?

0 Kudos
lelaharrington
Regular Contributor

Did anyone get an answer to this. I am having the same issue however I am not using a search cursor at all I am using a delete  feature.. it is deleting one feature (DB2) and then moving feature 1 DB1 to DB2 where the previous feature was deleted. I keep getting this error as well. it makes no sense.

0 Kudos
SugihonoBudiman
Emerging Contributor

To anyone looking for answer. Its a bug. See below

Synopsis

The arcpy.Delete_management (sde connection) function fails with the error, "000601: Cannot delete sde connection. May be locked by another application," in ArcMap 10.2.1.

Additional Information

N/A

Alternate Solution

Use os.remove() instead

NIM101220 - The arcpy.Delete_management (sde connection) funct..

AliNazari
Occasional Contributor

This is True.

I had to delete the .gdb where it was happening and recreate it to be able to solve the issue! 

0 Kudos
SugihonoBudiman
Emerging Contributor

No good. Any patch coming soon?

Thanks

Sugihono Budiman

Software Architect

• 917.873.5949

• sugihono.budiman@pcfcorp.com

Publishers Circulation Fulfillment, Inc.

www.pcfcorp.com

0 Kudos