If I run this code:
import arcpy import os def main(): in_gdb = 'test_arc_interface.gdb' in_features = os.path.join(in_gdb, 'section_grid') in_raster = os.path.join(in_gdb, 'bitumen') arcpy.ResetEnvironments() arcpy.CheckOutExtension('Spatial') arcpy.env.workspace = in_gdb f_raster = arcpy.PolygonToRaster_conversion(in_features, 'OBJECTID', 'in_memory/f_raster', 'MAXIMUM_AREA') table = arcpy.sa.ZonalStatisticsAsTable(f_raster, 'Value', in_raster, 'in_memory/r_table') print(arcpy.Delete_management(table)) print(arcpy.Delete_management(f_raster)) print(arcpy.Exists(table)) print(arcpy.Exists(f_raster)) arcpy.env.workspace = 'in_memory' print(arcpy.ListRasters()) print(arcpy.ListFeatureClasses()) if __name__ == '__main__': main()
I get:
true true False True ['f_raster'] []
i.e. Delete_management(f_raster) returns true, Exists(f_raster) returns false, but arcpy still lists it in ListRasters(). What is going on?
I suspect that it is held on to and treated as intermediate data until such time as the script is finished
http://pro.arcgis.com/en/pro-app/tool-reference/appendices/using-the-in-memory-output-workspace.htm
which might account for its presence. Perhaps try saving the in-memory version to disk and see if delete works then
Thanks for the quick reply!
I'm not entirely sure how I would 'save the in-memory version to disk' (I'm new to ArcGIS, sorry), but I can run something like this:
f_raster_disk = arcpy.Raster(f_raster) f_raster_disk.save(os.path.join(in_gdb, 'f_raster'))
and then Exists(f_raster_disk) works as expected (Exists(f_raster) still returns True, though).
I have found that arcpy.Exists isn't reliable. os.path.exisits is. But, arcpy.Delete_management won't error out if you provide a feature class or raster that does not exist.
I don't think my problem is that Exists is unreliable, though. I think the raster somehow still exists after I delete it, because, for example, if I call Delete_management on it a second time, I get "arcgisscripting.ExecuteError: ERROR 000601: Cannot delete in_memory\f_raster. May be locked by another application."
Could have been worse...
basically in-memory is a bit of a misnomer for most operations. If you can run timing functions on your scripts you will generally see that.