2

I have a custom python tool which creates a number of rasters and writes them to C:/Documents/ScriptTool folder as tif files, and also adds them as layers to the active map.

At the startup of the tool, I remove all layers from the map via:

aprx = arcpy.mp.ArcGISProject("CURRENT")
activeMap = aprx.activeMap
layers = activeMap.listLayers()

for layer in layers:
    if not "Map" in layer.longName:
        activeMap.removeLayer(layer)
aprx.save()

And I attempt to delete all files which are in the C:/Documents/ScriptTool folder, because I do not want them to linger for memory concerns and also because ArcGIS likes to crash if you try to do so.

try:
    shutil.rmtree("C:/Documents")
except:
    arcpy.AddMessage("Failed to delete all files.")

However, the .tif files themselves are not being deleted (Though the .tif.aux files and the .tfw files are). When I attempt to manually delete the files through the file explorer, I'm given the message: The action can't be completed because the file is open in ArcGIS Pro., despite having removed those layers from my active map.

If I close and re-open ArcGIS Pro, I'm able to delete the files and run my tool once again, but obviously this is non-ideal. Can I release the hold ArcGIS Pro has on these files so they may be deleted, programmatically?

I have also tried using arcpy.Delete_management() to remove the layers, with identical results.

  • 1
    I believe closing the activemap and creating a new activemap should release them. -- something similar to these steps should be python-able. – JasonInVegas Jul 10 '18 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.