[GIS] Deleting or replacing existing table in ArcPy gives ERROR 000601

arcgis-10.2arcpydeleteerror-000601lock

I'm doing an interface with ArcGIS 10.2 ando Python 2.7 scripts.
I can create new tables with arcpy, but when I run a second time, to replace created tables I need to delete the existing tables to create news with the same name. So I was using a code that gives me error, because I think that delete function doesn't delete the table, as I thought. I thought that problem could be because os memory cache, so I used a function to clear it, but it doesn't work..

My Python code:

tab_cg_r="F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf"
arcpy.env.workspace = "F:\ArcGIS\Trab\Model\Result_otim"
arcpy.ClearWorkspaceCache_management()

if arcpy.Exists("C_R_otim_view"):
    arcpy.Delete_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf")
    #arcpy.DeleteFeatures_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf\C_R_otim_view")

arcpy.env.workspace = "F:\ArcGIS\Trab\Model\Result_otim"
arcpy.ClearWorkspaceCache_management()

arcpy.CreateTable_management('F:\ArcGIS\Trab\Model\Result_otim', "C_R_otim.dbf")

# Process: Add Field
arcpy.AddField_management(tab_cg_r, "Barr", "Text", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Pt_M", "Float", "", "", 10, "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Longitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Latitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.DeleteField_management(tab_cg_r, 'Field1')
##
rows_t_cR = arcpy.InsertCursor("F:\ArcGIS\Trab\Model\Result_otim\C_R_ot.dbf")
for x in range(0, (len(N_name_LR))):
    row = rows_t_cR.newRow()
    rows_t_cR.insertRow(row)

arcpy.MakeTableView_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf", "C_R_otim_view")

Error:

ERROR 000601: Cannot delete F:\\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf.  May be locked by another application. Failed to execute (Delete).

How can I do this?

Best Answer

By adding import arcpy to the beginning of your code, and replacing your *.dbf with a test DBF file, I tested your code and was not able to reproduce your error message:

import arcpy

tab_cg_r="C:\Temp\Test.dbf"
arcpy.env.workspace = "C:\Temp"
arcpy.ClearWorkspaceCache_management()

if arcpy.Exists("C_R_otim_view"):
    arcpy.Delete_management("C:\Temp\Test.dbf")

arcpy.env.workspace = "C:\Temp"
arcpy.ClearWorkspaceCache_management()

arcpy.CreateTable_management('C:\Temp', "Test.dbf")

# Process: Add Field
arcpy.AddField_management(tab_cg_r, "Barr", "Text", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Pt_M", "Float", "", "", 10, "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Longitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Latitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.DeleteField_management(tab_cg_r, 'Field1')
##
rows_t_cR = arcpy.InsertCursor("C:\Temp\Test.dbf")
for x in range(0, (len(N_name_LR))):
    row = rows_t_cR.newRow()
    rows_t_cR.insertRow(row)

arcpy.MakeTableView_management("C:\Temp\Test.dbf", "C_R_otim_view")

Instead I got:

Traceback (most recent call last):
  File "C:\Temp\test.py", line 23, in <module>
    for x in range(0, (len(N_name_LR))):
NameError: name 'N_name_LR' is not defined

I could keep fixing things as I find them by guessing what you may be doing but I think the onus should be on you to provide us with code snippets that work up to where you are stuck.

In any event, I think the problem may be that you are running your code when you have another application - perhaps ArcMap, perhaps a Python IDE, or perhaps something else holding a lock on your F:\\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf file. This is indicated in the error message that you received which included:

May be locked by another application

I recommend closing all applications, even rebooting if this particular error persists, and then running just your test. That way there should be only the one application running.

I do not think that you are seeing ERROR 000601 for the same reason as Using os.remove in ArcGIS Python script tool? because that question is using ArcSDE when there is no suggestion in your question that you may be too. ClearWorkspaceCache is only used with enterprise geodatabases (ArcSDE) and so is not necessary in your code.