<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Cleaning up temporary layers created by python toolbox script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44763#M3529</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just call this def() before/after any in_memory processing.&amp;nbsp; Seems legit. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

def clearINMEM():


&amp;nbsp; arcpy.env.workspace = r"IN_MEMORY"

&amp;nbsp; fcs = arcpy.ListFeatureClasses()
&amp;nbsp; tabs = arcpy.ListTables()

&amp;nbsp; ### for each FeatClass in the list of fcs's, delete it.
&amp;nbsp; for f in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("deleted: " + f)
&amp;nbsp; ### for each TableClass in the list of tab's, delete it.
&amp;nbsp; for t in tabs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(t)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("deleted: " + t)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:43:15 GMT</pubDate>
    <dc:creator>JamesCrandall</dc:creator>
    <dc:date>2021-12-10T21:43:15Z</dc:date>
    <item>
      <title>Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44756#M3522</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ArcMap 10.2.1 / Python 2.7&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have several tools in a python toolbox. When I run through the tools in succession, temporary layers created in previously run tools show up as parameters in later tools.&amp;nbsp; These temporary layers are created with arcpy.MakeFeatureLayer_management and the corresponding feature classes are in the scratch workspace.&amp;nbsp; The layers show up in other tools for parameters where parameterType = GPFeatureLayer. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example: Tool_A creates temporary layers temp1 and temp2 and then adds "Final Layer" to the TOC.&amp;nbsp; If I run Tool_B which takes a layer parameter, I would expect "Final Layer" to show up in the list but NOT layers temp1 &amp;amp; temp2.&amp;nbsp; The behavior I am seeing however is that all three layers show up as choices.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am setting arcpy.env.addOutputsToMap = False at the beginning of each tool script and the temporary layers do not show up in the TOC as expected.&amp;nbsp; I also delete all layers from scratch at the end of each tool script.&amp;nbsp; Any help would be greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 14:24:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44756#M3522</guid>
      <dc:creator>JohnJackson</dc:creator>
      <dc:date>2014-03-24T14:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44757#M3523</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Maybe try arcpy.RefreshTOC() and arcpy.RefreshActiveView() after you delete the temporary layers?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 14:53:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44757#M3523</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-03-24T14:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44758#M3524</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Adam.&amp;nbsp; I am actually calling both functions when I add the final layer to the map.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 15:14:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44758#M3524</guid>
      <dc:creator>JohnJackson</dc:creator>
      <dc:date>2014-03-24T15:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44759#M3525</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hmm, it may help to add those operations to the initializeParameters() method in the ToolValidator class for each tool, so that the parameters are definitely referencing an up-to-date mxd. Otherwise storing those temp feature classes in "in_memory\" instead of a scratch gdb may help too.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 15:33:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44759#M3525</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-03-24T15:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44760#M3526</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Strange. I thought in-memory objects like layers and in_memory feature classes are deleted when a script finishes, but anyway...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Make sure that by the end of each script you delete the temporary feature classes (the actual data) as well as the temporary layers (in-memory objects pointing to the data).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would love to hear how you guys do this and if there is good practice that really works. So far in my scripts I have been doing something like this (pseudo-code):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy # start of script&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; todlt = [] # list to store everything I want to delete in the end. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # ... body of the script, whatever ...&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; my_buffer = arcpy.Buffer_analysis(...).getOutput(0) &amp;nbsp;&amp;nbsp;&amp;nbsp; todlt.append(my_buffer) # if my_buffer is a temporary feature class &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; my_buffer_lyr = arcpy.MakeFeatureLayer_management(my_buffer, 'lyrname', ...).getOutput(0) &amp;nbsp;&amp;nbsp;&amp;nbsp; todlt.append(my_buffer_lyr) # my_buffer_lyr is a temporary feature layer&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # ... rest of the body of script ... &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except arcpy.ExecuteError as e: &amp;nbsp;&amp;nbsp;&amp;nbsp; # handle ... except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp; # handle ... finally: &amp;nbsp;&amp;nbsp;&amp;nbsp; # cleanup &amp;nbsp;&amp;nbsp;&amp;nbsp; if 'todlt' in dir(): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for dl in todlt: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(dl) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning('Could not delete temporary data ' + str(dl))&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-size:1;"&gt;By the way, I often use functions from my arcapi repository you may want to check out: &lt;A class="jive-link-external-small" href="https://github.com/NERC-CEH/arcapi" rel="nofollow" target="_blank"&gt;https://github.com/NERC-CEH/arcapi&lt;/A&gt;&lt;BR /&gt;Specifically, function dlt() comes handy for the cleanup:&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;if 'todlt' in dir(): &amp;nbsp;&amp;nbsp;&amp;nbsp; for dl in todlt: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ap.dlt(dl)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;and function tstamp is useful to generate temporary layer names:&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;my_buffer_lyr = arcpy.MakeFeatureLayer_management(my_buffer, "lr" + ap.tstamp(tf = "%H%M%S") , ...).getOutput(0)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Filip.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Mar 2014 22:36:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44760#M3526</guid>
      <dc:creator>FilipKrál</dc:creator>
      <dc:date>2014-03-24T22:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44761#M3527</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Filip!&amp;nbsp; After looking at your code and re-reading the documentation on Internal Layers &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00210000000m000000" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//00210000000m000000&lt;/A&gt;&lt;SPAN&gt;, specifically:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;When you create a layer or table view using geoprocessing tools, the new layer or table view is stored in an internal layer list, which is a different list from the ArcMap table of contents. This means that geoprocessing actually keeps two lists of layers and table views:&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;The list of layers in the ArcMap table of contents&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;The internal list of layers created by geoprocessing tools&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;I can see what I was doing wrong.&amp;nbsp; I &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;thought &lt;/STRONG&gt;&lt;SPAN&gt;I was deleting the temporary layers by getting a list of layers for the data frame and deleting from that list:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def clearLayers():
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument('CURRENT')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for df in arcpy.mapping.ListDataFrames(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(df, lyr) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This was not clearing the internal layer list (layers created by other geoprocessing tools) that the python tools use in part to generate parameter lists.&amp;nbsp; Having &lt;/SPAN&gt;&lt;STRONG&gt;arcpy.env.addOutputsToMap = False&lt;/STRONG&gt;&lt;SPAN&gt; had no effect on the internal layer list either (which it shouldn't).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:43:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44761#M3527</guid>
      <dc:creator>JohnJackson</dc:creator>
      <dc:date>2021-12-10T21:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44762#M3528</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is worth discussing further. The help is not explicit about this and it's too bad -- if you don't clean up your layers, you leave joins and file locks polluting your session -- especially with script tools launched in-process from ArcGIS Desktop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's the approach that I have been using.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14108018472604902 jive_text_macro" jivemacro_uid="_14108018472604902"&gt;
&lt;P&gt;from datetime import datetime&lt;/P&gt;
&lt;P&gt;dtag = datetime.now().strftime("%Y%m%d%H%M%S") # '20140915110231'&lt;/P&gt;
&lt;P&gt;lyrName = "lyr" + dtag&lt;/P&gt;
&lt;P&gt;# set up temp variables&lt;/P&gt;
&lt;P&gt;tmp_layer, tmp_table = [None] * 2&lt;/P&gt;
&lt;P&gt;try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ... create tmp_table name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tmp_table = arcpy.CreateScratchName("", dtag, "table")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CreateTable_management(os.path.dirname(tmp_table),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.path.basename(tmp_table))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tmp_layer = arcpy.MakeFeatureLayer(tmp_table, lyrName)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...&lt;/P&gt;
&lt;P&gt;except:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass # normal error handling here&lt;/P&gt;
&lt;P&gt;finally:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for f in [tmp_layer, tmp_table]:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if f:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(f)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;




&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note - we don't need to clean up lyrName because it's just a simple string object that will be deleted when the python script ends. The variable tmp_layer is assigned a result object returned from MakeFeatureLayer, but when you use it as input to a tool (like Delete_management) the tool is only passed the result object's string representation, which is just the string (above, "lyr201409015110231", or, below, "lyrname") so we don't need to unpack it with result.getOutput(0).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are some details, shown from the Python prompt (in ArcMap). Gotta love it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14108022902163864 jive_text_macro" jivemacro_uid="_14108022902163864"&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; lyr = arcpy.MakeFeatureLayer_management(r"temp.shp", "lyrname")&lt;/P&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; type(lyr)&lt;/P&gt;
&lt;P&gt;&amp;lt;class 'arcpy.arcobjects.arcobjects.Result'&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; lyr&lt;/P&gt;
&lt;P&gt;&amp;lt;Result 'lyrname'&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; str(lyr)&lt;/P&gt;
&lt;P&gt;'lyrname'&lt;/P&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; lyr.getOutput(0)&lt;/P&gt;
&lt;P&gt;&amp;lt;map layer u'lyrname'&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.Delete_management(lyr)&lt;/P&gt;
&lt;P&gt;&amp;lt;Result 'true'&amp;gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-size: 9pt; line-height: 12pt;"&gt;&amp;gt;&amp;gt;&amp;gt; print(arcpy.GetMessages())&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Executing: Delete lyrname #&lt;/P&gt;
&lt;P&gt;Start Time: Mon Sep 15 11:32:05 2014&lt;/P&gt;
&lt;P&gt;Succeeded at Mon Sep 15 11:32:05 2014 (Elapsed Time: 0.00 seconds)&lt;/P&gt;



&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 17:25:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44762#M3528</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-15T17:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44763#M3529</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just call this def() before/after any in_memory processing.&amp;nbsp; Seems legit. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

def clearINMEM():


&amp;nbsp; arcpy.env.workspace = r"IN_MEMORY"

&amp;nbsp; fcs = arcpy.ListFeatureClasses()
&amp;nbsp; tabs = arcpy.ListTables()

&amp;nbsp; ### for each FeatClass in the list of fcs's, delete it.
&amp;nbsp; for f in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("deleted: " + f)
&amp;nbsp; ### for each TableClass in the list of tab's, delete it.
&amp;nbsp; for t in tabs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(t)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("deleted: " + t)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:43:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44763#M3529</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-10T21:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44764#M3530</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;James, I believe we are discussing &lt;EM&gt;layers&lt;/EM&gt;, not datasets (in_memory, on disk, or in an RDBMS). I realize I confused the thread a bit by including a table in my code sample. If you delete a feature class that is referenced by a open layer in memory, the Delete_management will not throw an error but the dataset will persist until the python session (not the script) is over. At least that's my experience.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For this reason, you'll see in my finally block that I listed layer first, &lt;EM&gt;then&lt;/EM&gt; the dataset in my delete list.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 18:32:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44764#M3530</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-15T18:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44765#M3531</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah.&amp;nbsp; Your Delete_management of FeatureLayers works for me too.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 18:54:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44765#M3531</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-15T18:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44766#M3532</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Aha, that's a good point about the locks, Curtics. So it's best to delete the layer as soon as you are done doing what you needed the layer (or table view) for.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 19:25:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44766#M3532</guid>
      <dc:creator>FilipKrál</dc:creator>
      <dc:date>2014-09-15T19:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44767#M3533</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is all great info, and I especially like the delete everything in memory function.&amp;nbsp; I thought I'd throw my own two cents in also...&amp;nbsp; I began to deal with deleting feature classes like this a while ago by creating my own little python module.&amp;nbsp; It's super useful to have a some functions that you can call anytime from anywhere, because I need to do this "delete if exists" thing all the time (plus a lot of other stock operations that I'm tired of retyping).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just make a folder in your python site-packages directory (something like C:\Python2x\Lib\site-packages, or, more likely, C:\Python2x\ArcGIS10.x\Lib\site-packages) called mypack, and then in that folder create a text file called __init__.py.&amp;nbsp; You can define all the functions you want in there, and to access them from any console or script just say "from mymod import functionname".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For what we're talking about here, I just have this in the __init__.py file:&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;def TakeOutTrash(dataset):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if arcpy.Exists(dataset):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.Delete(dataset)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so in the console, making a feature layer could look like:&lt;/P&gt;&lt;P&gt;from mypack import TakeOutTrash&lt;/P&gt;&lt;P&gt;fl = "fl"&lt;/P&gt;&lt;P&gt;TakeOutTrash(fl) # this is important if you are iterating or running the script many times&lt;/P&gt;&lt;P&gt;arcpy.management.MakeFeatureLayer(r"path",fl)&lt;/P&gt;&lt;P&gt;##do some stuff with feature layer&lt;/P&gt;&lt;P&gt;TakeOutTrash(fl)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That function could be modified to accept a list of datasets too, I just haven't done that yet.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 19:32:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44767#M3533</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-09-15T19:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44768#M3534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do you copy and paste this code into the console, or run it from somewhere else? See my post below; it'll be a good thing for me to add to my python package...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 19:42:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44768#M3534</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-09-15T19:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning up temporary layers created by python toolbox script</title>
      <link>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44769#M3535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Adam,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I include that cleanup def() in just about all of my .py source files to any of the Geoprocess tools I build that utilize in_memory space.&amp;nbsp; But like Curtis said, it's a little off topic to this thread.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Sep 2014 20:19:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cleaning-up-temporary-layers-created-by-python/m-p/44769#M3535</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-09-15T20:19:50Z</dc:date>
    </item>
  </channel>
</rss>

