Select to view content in your preferred language

can't delete temporary layer in Python

683
5
10-24-2024 12:16 PM
PaulCone2
Frequent Contributor

I have temporary layer that I'm creating in a Pro Notebook. 

arcpy.MakeFeatureLayer_management(boec, r"memory\boecFL")

Once I'm done processing, I'm attempting to delete it, but I get an error message.  If I manually delete it, it works, but then when I copy that from the History as a Python snippet and try and run it, it fails.

arcpy.management.Delete(
in_data=r"memory\boecFL",
data_type="GPFeatureLayer"
)

WARNING 000110: memory\boecFL does not exist

I've tried various permutations for in_data and data_type.  Running Pro 3.1.7.  What am I missing?

 

 

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

it is deleted when the application is closed, as per help topic

and it should be deleted as soon as possible within your script, not after it has run, since references to it will be lost when the script finishes and the application is still open

Write geoprocessing output to memory—ArcGIS Pro | DocumentationWrite geoprocessing output to memory—ArcGIS Pro | Documentation

 


... sort of retired...
0 Kudos
PaulCone2
Frequent Contributor

Nah, it puts it in the TOC, and I attempt to delete it pretty soon after.  When I close the project and reopen it, it's still there.  

memory.png

print("Making feature layer from " + boec)
arcpy.MakeFeatureLayer_management(boec, r"memory\boecFL")
toc = time.process_time()
print("Making feature layer process time = " + str(toc - tic))

tic = time.process_time()
print("Selecting features from " + r"memory\boecFL" + " for " + expression)
arcpy.SelectLayerByAttribute_management(r"memory\boecFL", selection_type="NEW SELECTION", where_clause=expression)
toc = time.process_time()
print("Selecting features process time = " + str(toc - tic))

tic = time.process_time()
print("Inserting features from " + r"memory\boecFL" + " into " + outputFC_FM)
fcInsert(uidPreValue="BOEC", lookupValue="BOEC", featureLayer=r"memory\boecFL") ### Call the main fcInsert function
print("Deleting in memory feature layer")
arcpy.management.Delete(
in_data=r"memory\boecFL",
data_type=""
)

0 Kudos
PeterKnoop
MVP Regular Contributor

Make Feature Layer produces a feature layer, rather than a feature class, so out_layer cannot be directed to the "memory" workspace. 

Your call to MakeFeatureLayer is actually creating a feature layer that ends up with the name of "memory\boecFL".

If you examine the Properties for "memory\boecFL" in your table of contents, then you should see under Source that its Data Source is the original boec feature class from which it was derived, rather than a feature class named boecFL in the memory workspace.

You'll note the documentation for Make Feature Layer states, "The [output] layer that is created is temporary and will not persist after the session ends unless the layer is saved to disk or the map document is saved." I suspect this wording is a holdover from the days of ArcMap, and needs updating for Pro. 

In Pro, the temporary output layer generated by Make Feature Layer is persisted when you save your project. So the only way to not persist the layer is to close Pro without saving your project, after you've run Make Feature Layer.

(Side note: depending on what your goal is here, the SelectLayerByAttribute step may be extraneous. You can specify an expression directly in MakeFeatureLayer. In which case the resulting temporary layer would be ready for your insert step as-is.)

0 Kudos
PaulCone2
Frequent Contributor

Thank you, this is helpful.  My goal is to get rid of the extra layer and symbology that is junking up the map, and not the data behind it.

The expression is built in some code prior so I think it would be unwieldy to put it directly in the MakeFeatureLayer call.  Here's the entire code block...

#region insert BOEC data
tic = time.process_time()
print("** Inserting data from BOEC source layer ** ")
boec = inFC
expression = "{} IN ('FRVW', 'GRSM', 'MULT', 'MYDP', 'PORT', 'TRO', 'WVLG')".format(arcpy.AddFieldDelimiters(r"memory\boecFL", "AGENCY"))
print("Making feature layer from " + boec)
arcpy.MakeFeatureLayer_management(boec, r"memory\boecFL")
toc = time.process_time()
print("Making feature layer process time = " + str(toc - tic))

tic = time.process_time()
print("Selecting features from " + r"memory\boecFL" + " for " + expression)
arcpy.SelectLayerByAttribute_management(r"memory\boecFL", selection_type="NEW SELECTION", where_clause=expression)
toc = time.process_time()
print("Selecting features process time = " + str(toc - tic))

tic = time.process_time()
print("Inserting features from " + r"memory\boecFL" + " into " + outputFC_FM)
fcInsert(uidPreValue="BOEC", lookupValue="BOEC", featureLayer=r"memory\boecFL") ### Call the main fcInsert function
print("Deleting in memory feature layer")
arcpy.management.Delete(
in_data=r"memory\boecFL",
data_type=""
)
toc = time.process_time()
print("Inserting fields process time = " + str(toc - tic))
#endregion

0 Kudos
PeterKnoop
MVP Regular Contributor

You can remove the layer MakeFeatureLayers is creating using the removeLayer method for your Map.

0 Kudos