Remove a Layer usinng Python

Discussion created by jmillanq on Oct 8, 2011
Latest reply on Oct 10, 2011 by Hornbydd
[FONT="Courier New"]Hi, I'm looping over network analysis. To be more precise I'm finding the best route from 165 starting points to 56 ending points.
The python script runs well but given that it creates a layer for each set of stops and for each final route the program collapses after some time. I suppose that the problem is that the virtual memory of ArcGis collapse or that I reach the maximum number of active layers in ArcGis. I would like to know how to remove a layer from the active workspace or how to clean the virtual memory of ArcGis using a Python script.
Thanks in advance

This is the Python script I'm using

# Declare: Folder Paths
path = "E:\PhD Papers\Fertility decision during conflict\MapsAnalysis\AnalysisDDRoutes110922\\"
# The path is the key local to change in order to run the same program over
# different folders.
path_origin = path + "Origins\\"
path_dest = path + "Destinations\\"
path_route = path + "RoutesFinal\\"
path_stops = path + "Stops\\"
targetLyr = "Municipios"
finalOutput = "routesMnpAll"

# Declare: Network Analysis Locals
MRoute = "MasterRoute" # I can solve over one Make a route layer
inputLyr = MRoute + "/Routes"
Active_Net = "Roads/vias_unificado_ND" # Active network
hrch = "USE_HIERARCHY" # Declare Hierarchy
cost = "Length" # Cost variable#

# Declare: Program local variables
Org = "orgRoads2002"
Dest = "destRoads"

# Call the packages
import arcpy, os # Argis
from arcpy import env # Import environments
env.workspace = path + "probando.mxd"

# Check out any necessary licenses

# Process: Make Route Layer
arcpy.MakeRouteLayer_na(Active_Net, MRoute, cost, op1, op2, op3, "", op4, "",hrch, "", op5, "")

for x in range(10,11 ):
for y in range(0,1):

# Declare Local Variables
selectOrg ="FID=" + str(x)
selectDest ="FID=" + str(y)
stops = "stops_" + str(x) + "_" + str(y)
stopsLyr = path_stops + stops
outRuta = "Route_"+str(x)+"_"+str(y)
routeMnp = outRuta + "Mnp"
out_routeMnp = path_route + routeMnp
rutaVar = "'" + outRuta + "'"
vLocal = "selectOrg, selectDest, stops, stopsLyr, outRuta, routeMnp, out_routeMnp, rutaVar"

#Process: Select Origin and Destination
arcpy.SelectLayerByAttribute_management(Org, "NEW_SELECTION", selectOrg)
arcpy.SelectLayerByAttribute_management(Dest, "NEW_SELECTION", selectDest)

#Process: Create the stops
arcpy.CopyFeatures_management(Org, stopsLyr)
arcpy.Append_management(Dest, stops)

# Process: Add stops
arcpy.AddLocations_na(MRoute, "Stops", stops, "Name Name #", "5000 Meters","","","","CLEAR")

# Process: Solve
arcpy.Solve_na(MRoute, "SKIP", "TERMINATE")

# Process: Select by location and create new layer
arcpy.CopyFeatures_management(targetLyr, out_routeMnp)

# Process: Add Field with the route name

print "Error -> " + outRuta +" doesn't exist"

del vLocal