How to delete memory-workspace

2222
14
04-02-2020 11:25 PM
MattWeber2
New Contributor II

Hello,

how can I delete the memory-workspace after a script has run?

I usually added

arcpy.Delete_management("memory")

to the end of my scripts, but noticed yesterday that this apparently doesn't do anything. 

The workaround i found is to loop over all fc in the workspace and delete them one by one.

for fc in arcpy.ListFeatureClasses():
    desc = arcpy.Describe(fc)
    arcpy.Delete_management(desc.catalogPath)

But this help site indicates it should be possible to delete all at once.

Write geoprocessing output to memory—ArcGIS Pro | Documentation 

Also, the older in_memory seems to empty itself after a script ran, why doesn't memory do the same?

Cheers

edit: I am using ArcGIS Pro 2.4.3

14 Replies
DanPatterson_Retired
MVP Esteemed Contributor

The help doesn't differentiate between in_memory and memory for deleting, as you have noted.

I couldn't find anything related to this on the support site (visible to mere mortals), but I stumbled on this

BUG-000120068: Loading data into a memory-workspace consumes about .. 

Perhaps more lurks behind the (in_)memory divide.

JoshuaBixby
MVP Esteemed Contributor

how can I delete the memory-workspace after a script has run?

How exactly are you running your script?  As a script tool from within Pro, as a stand-alone script using Pro conda?

0 Kudos
MattWeber2
New Contributor II

I run it as a script tool from within Pro.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

The full script, might reveal more

0 Kudos
MattWeber2
New Contributor II
arcpy.AddMessage(arcpy.Exists(r"memory\copied"))
arcpy.CopyFeatures_management("to_copy", "memory\copied")
arcpy.AddMessage(arcpy.Exists(r"memory\copied"))
arcpy.Delete_management("memory")
arcpy.AddMessage(arcpy.Exists(r"memory\copied"))
‍‍‍‍‍‍‍‍‍‍‍

Output is 

False, True, True the first time the script runs, True True True the second time and onwards

So line 4 doesn't do anything, and even after the Tool finished, fc in memory persist.

Replacing line 4 with the specific delete-call:

arcpy.Delete_management(r"memory\copied")

gives the expected result of False, True, False for any number of runs

If I use the older "in_memory" it works fine by the way.

arcpy.AddMessage(arcpy.Exists(r"in_memory\copied"))
arcpy.CopyFeatures_management("to_copy", "in_memory\copied")
arcpy.AddMessage(arcpy.Exists(r"in_memory\copied"))
arcpy.Delete_management("in_memory")
arcpy.AddMessage(arcpy.Exists(r"in_memory\copied"))‍‍‍‍‍‍‍‍‍‍

Output is:

False, True, False

And even if I remove line 4 and 5 the output is

False, True

for any number of runs. So the temporary fc saved in_memory are not kept in between runs.

JoshuaBixby
MVP Esteemed Contributor

What version of Pro are you running?  I just tested your first code block as a script tool in ArcGIS Pro 2.5, and it returns False, True, False every time I run it.

MattWeber2
New Contributor II

I am running 2.4.3.

Since nobody else can reproduce the behavior, I will resort to the workaround of deleting everything one by one for now.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Run from Spyder (IPython console).

dump AddMessage.... use print to rule it out as a culprit

# ---- test featureclass

copied = r"C:\Git_Dan\npgeom\Project_npg\tests.gdb\sq"

# ---- results for 'memory'
print(arcpy.Exists(r"memory\copied"))
arcpy.CopyFeatures_management(copied, "memory\copied")
print(arcpy.Exists(r"memory\copied"))
arcpy.Delete_management("memory")
print(arcpy.Exists(r"memory\copied"))

False
True
False

# ---- results for 'in_memory'
print(arcpy.Exists(r"in_memory\copied"))
arcpy.CopyFeatures_management(copied, "in_memory\copied")
print(arcpy.Exists(r"in_memory\copied"))
arcpy.Delete_management("in_memory")
print(arcpy.Exists(r"in_memory\copied"))
False
True
False‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Executable for the above

sys.executable
Out[9]: 'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\pythonw.exe'

Run from Python window in Pro

import arcpy
copied = r"C:\Git_Dan\npgeom\Project_npg\tests.gdb\sq"
# ---- results for 'memory'
print(arcpy.Exists(r"memory\copied"))
arcpy.CopyFeatures_management(copied, r"memory\copied")
print(arcpy.Exists(r"memory\copied"))
arcpy.Delete_management("memory")
print(arcpy.Exists(r"memory\copied"))
False
True
False

# ---- results for 'memory'
print(arcpy.Exists(r"memory\copied"))
arcpy.CopyFeatures_management(copied, r"memory\copied")
print(arcpy.Exists(r"memory\copied"))
arcpy.Delete_management("memory")
print(arcpy.Exists(r"memory\copied"))
False
True
False

the 'executable' from the python window inpro

import sys
sys.executable
'C:\\arc_pro\\bin\\ArcGISPro.exe'

Use print

MattWeber2
New Contributor II

It works in IPython

sys.executable
'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe'

but the same result in ArcGIS, Python window

or Toolbox

sys.executable
'C:\\Program Files\\ArcGIS\\Pro\\bin\\ArcGISPro.exe'