Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an ArcPy based script where I'm trying to intersect two layers where one layer must have its centroid in the other layer.

Since I need to know that one layer (feature_layer below) includes the centroid of the other, I'm using the arcpy.SelectLayerByLocation_management() tool.

I do not have a Map Document (*.mxd) open so in order to do this, I have to use Make Feature Layer to feed into it.

The documentation notes that the feature layer will disappear when the program exits, but I need it to disappear sooner since I have it in a loop similar to below

for feature_class in large_list_of_feature_classes:

def some_function():
    feature = "{path to feature class}"
    feature_layer = "feature_layer"
    HUCs_layer = "all_HUCs" #HUCs being Hydrologic Unit Codes

    arcpy.MakeFeatureLayer_management(feature, feature_layer)
    arcpy.MakeFeatureLayer_management(feature, HUCs_layer)      
    arcpy.SelectLayerByLocation_management(HUCs_layer, "HAVE_THEIR_CENTER_IN", feature_layer, selection_type="NEW_SELECTION")

    arcpy.CopyFeatures_management(HUCs_layer, selection_name)

I'm looking for a function to remove feature layers and I haven't been able to find one.

I realize I could put some sort of count in there or something to make each one unique within the scope of the program, but that's not ideal unless there is no way to remove feature layers.

share|improve this question
up vote 17 down vote accepted

The Delete tool should accept the name of a feature layer to remove it from the TOC.

Permanently deletes data from disk. All types of geographic data supported by ArcGIS, as well as toolboxes and workspaces (folders, geodatabases), can be deleted. If the specified item is a workspace, all contained items are also deleted.

and under the Syntax section lists the Data Types that can be deleted as:

Data Element; Graph; Layer; Table View

share|improve this answer
Thanks Jason - worked like a charm. When I get enough reputation, I'll thank you with an upvote. – nicksan Feb 15 '11 at 18:55
i don't know why i should use the delete tool twice to be sure that the feature layers is removed, and sometimes this function gimme errors, despite the feature layer not really removed , this errors occurs when i try to delete and create feature layers with the same name – geogeek Jun 22 '12 at 19:43
This isn't really related, but it's the way geoprocessing works -- it expects each layer to have a unique name because that's how it refers to them. – Jason Scheirer Jun 23 '12 at 22:30
Perhaps the use of the Delete tool to remove a layer from the TOC could be better documented at… - it says Layer is a data type that can be deleted, but no other references are made to layers and its opening sentence "Permanently deletes data from disk" would scare many users away from trying Delete to remove a layer. – PolyGeo Aug 14 '12 at 0:57

You can invoke arcpy.RemoveLayer to remove a layer from the TOC.

Provides the ability to remove a layer within a data frame in a map document (.mxd).

share|improve this answer
Thanks for the suggestion - I didn't clarify that I was looking for something to do this outside of the mapping context - this is just an analysis script and I don't have (or at least don't know) an explicit TOC. Jason's suggestion below worked though. Thanks! It'll still be a useful function for me later on. – nicksan Feb 15 '11 at 19:02

Use if you're using a map document that is not CURRENT to make sure the layer stays gone. Also, add a print statement in your script:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

for i in arcpy.mapping.ListLayers(mxd , "TerraColor_SanFrancisco_US_15m.tif"):
    print "Deleting layer", i
    arcpy.mapping.RemoveLayer(df , i)


Just to make sure it's finding a layer to delete at all.

share|improve this answer

This is what I use to remove layers.

arcpy.mapping.RemoveLayer("DataFrame", "Layer Name")

Or looping:

mxd = arcpy.mapping.MapDocument("CURRENT")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        arcpy.mapping.RemoveLayer(df, lyr)
share|improve this answer

try this:

import arcpy
from arcpy import env
env.workspace = r"F:\Projects\ohad\derech_atankim\gis"   
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"F:\Projects\ohad\derech_atankim\gis\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if lyr.dataSource == r"F:\Projects\ohad\derech_atankim\gis\layers\roads.lyr":
            arcpy.mapping.RemoveLayer(df, lyr)
del mxd
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.