Cleaning up temporary layers created by python toolbox script

16852
13
Jump to solution
03-24-2014 07:24 AM
JohnJackson
New Contributor
ArcMap 10.2.1 / Python 2.7
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.  These temporary layers are created with arcpy.MakeFeatureLayer_management and the corresponding feature classes are in the scratch workspace.  The layers show up in other tools for parameters where parameterType = GPFeatureLayer.

For example: Tool_A creates temporary layers temp1 and temp2 and then adds "Final Layer" to the TOC.  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 & temp2.  The behavior I am seeing however is that all three layers show up as choices.

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.  I also delete all layers from scratch at the end of each tool script.  Any help would be greatly appreciated.
Tags (2)
0 Kudos
13 Replies
FilipKrál
Occasional Contributor III

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.

0 Kudos
AdamCox1
Occasional Contributor II

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...

0 Kudos
JamesCrandall
MVP Frequent Contributor

Adam,


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.  But like Curtis said, it's a little off topic to this thread.

0 Kudos
AdamCox1
Occasional Contributor II

This is all great info, and I especially like the delete everything in memory function.  I thought I'd throw my own two cents in also...  I began to deal with deleting feature classes like this a while ago by creating my own little python module.  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).

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.  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".

For what we're talking about here, I just have this in the __init__.py file:

import arcpy

def TakeOutTrash(dataset):

   if arcpy.Exists(dataset):

       arcpy.management.Delete(dataset)

so in the console, making a feature layer could look like:

from mypack import TakeOutTrash

fl = "fl"

TakeOutTrash(fl) # this is important if you are iterating or running the script many times

arcpy.management.MakeFeatureLayer(r"path",fl)

##do some stuff with feature layer

TakeOutTrash(fl)

That function could be modified to accept a list of datasets too, I just haven't done that yet.

0 Kudos