After cancelling with autoCancelling enabled arcpy.Delete_management() throws 000582 errors

564
0
12-18-2019 06:41 PM
HenryColgate
Occasional Contributor

When trying to use the arcpy.env.autoCancelling feature I seem unable to call arcpy.Delete_management() after cancellation without encountering a 000582 error.

The code below replicates the error when used as an ArcToolbox Python script if the Cancel button is pressed during the process.  The sleep delay is probably unnecessary but gives some breathing space. The code is a simplified and parameter/input free version of the ESRI example here. The workspace variable needs to be modified prior to set up.

import time
import arcpy

arcpy.env.autoCancelling = False

def my_function():
    temp_tables = ['replicant_1', 'replicant_2', 'replicant_3', 'replicant_4']

    workspace = r'C:/Users/username/Documents/ArcGIS/Default.gdb'
    try:
        for fc_name in temp_tables: 
            arcpy.CreateFeatureclass_management(workspace, fc_name, 'POINT')
            arcpy.AddMessage('WAITING')
            time.sleep(3)
            arcpy.AddMessage('FINISHED')
            if arcpy.env.isCancelled:
                raise Exception('Tool has been cancelled')
    except Exception as err:        
        arcpy.AddError(err)
    finally:
        # If tool is cancelled or finishes successfully, clean up 
        if temp_tables:
            for temp_table in temp_tables:
                arcpy.AddMessage(temp_table)
                try:
                    arcpy.Delete_management(temp_table)
                except Exception as err:
                    arcpy.AddError('DELETE FAILED')
                    arcpy.AddError(err)

if __name__ == '__main__':
    my_function()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've tried checking that the data exists and that there are no locks and neither of these seem to be the issue. I've also tried as background and foreground geoprocessing. It fails in both ArcMap 10.5 and ArcGIS Pro 2.4 (the code is good to switch between Python 2.x and 3.x)

Interestingly if I run the Toolbox again immediately after and at least one of the created files exists and arcpy.env.overwriteOutput is False this causes an error which is caught by the general exception handler and all of the existing files are deleted as expected. This indicates to me that the parameters are valid which is at least half the issue found in the 000582 Error Explanation so maybe it is the Execute Implementation?

0 Kudos
0 Replies