ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • Help
  • Sign Out
ArcGIS Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS Desktop

A complete professional GIS

ArcGIS Enterprise

GIS in your enterprise

ArcGIS Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

ArcMap

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • Extensions

RemoveLayer

  • Summary
  • Discussion
  • Syntax
  • Code sample

Summary

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

Discussion

RemoveLayer will remove a single layer or group layer from a specific data frame. If there is more than one layer that meets the criteria, then only the first layer will be removed unless the script iterates through each layer in a returned list.

Syntax

RemoveLayer (data_frame, remove_layer)
ParameterExplanationData Type
data_frame

A reference to a DataFrame object that contains the layer to be removed.

DataFrame
remove_layer

A reference to a Layer object representing the layer to be removed.

Layer

Code sample

RemoveLayer example:

The following script will remove all layers with the name Rivers from a map document.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if lyr.name.lower() == "rivers":
            arcpy.mapping.RemoveLayer(df, lyr)
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd
RemoveLayer example 2

The following script will remove a layer from a data frame called County Maps based on it's name and data source because other layers exist within the data frame with the same name but different source.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.name.lower() == "lakes":
        if lyr.dataSource == r"C:\Project\Data\Data.mdb\NE_Lakes":
            arcpy.mapping.RemoveLayer(df, lyr)
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd

ArcGIS Desktop

  • Home
  • Documentation
  • Support

ArcGIS

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS
  • ArcGIS Developer
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Esri Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
Copyright © 2021 Esri. | Privacy | Legal