How to Delete Temporary Feature Layers Not Loaded in Visible in MXD

456
2
Jump to solution
06-27-2020 06:16 AM
NealBanerjee
Occasional Contributor

Hello,

I have a series of models/scripts that create many temporary feature layers based on selection (using the MakeFeatureLayer tool),  Since they are within models and scripttools they are not automatically loaded/visualized in my MXD (which is what I want).

I was wonder if there is a way to programatically access and delete all these temporary layer files.  I know I can use 'Delete' to see them and delete them individually, but Im curious if there is a way to access in ArcPy so I could loop through all layers that end in "_Layer" and delete (see below.  I tried deleting everything in the 'in_memory' but that didnt remove them.

I do not want to delete the underlying feature class, just the instance of the layer.  When I manually delete the temporary layer using the delete tool, it does what I want (just deleting layer instance but maintaining data)

Any help would be appreciated

thanks!

Neal

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Frequent Contributor

Neal,

What you are doing is correct and what I would do, you run the delete tool and just give it the Layer name and that deletes the Layer that is in memory and not the source (Feature Class).  Deleting in_memory would only delete the Feature Class if your model had written to in_memory. Dan's code above is a "red herring", it is deleting the Feature Classes and not Layers created by the Make Feature Layer tool.

In model builder I would delete the layer as below, note the output of the selection (which in your case would undoubtedly have more tools attached to it) is a PRECONDITION, i.e. once all the processing is done delete the layer object.

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Neal Banerjee

You could probably cobble something together from the arcpy.walk example 2. and the 2nd from Delete_management.  Untested of course

import arcpy
import os

workspace = "c:/data"
to_del = []

walk = arcpy.da.Walk(workspace, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
if "_layer" in filename:
to_del.append(filename)
out = ";".join([n for n in to_del]) # ---- generate a semi-colon delimited list
arcpy.env.workspace = workspace
arcpy.management.Delete(out)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

... sort of retired...
0 Kudos
DuncanHornby
MVP Frequent Contributor

Neal,

What you are doing is correct and what I would do, you run the delete tool and just give it the Layer name and that deletes the Layer that is in memory and not the source (Feature Class).  Deleting in_memory would only delete the Feature Class if your model had written to in_memory. Dan's code above is a "red herring", it is deleting the Feature Classes and not Layers created by the Make Feature Layer tool.

In model builder I would delete the layer as below, note the output of the selection (which in your case would undoubtedly have more tools attached to it) is a PRECONDITION, i.e. once all the processing is done delete the layer object.

View solution in original post

0 Kudos