<?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: RemoveLayer - arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718406#M55655</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Read this...&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Fanalyze%2Farcpy-mapping%2Fremovelayer.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/removelayer.htm&lt;/A&gt;&lt;STRONG&gt;​&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then this&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Fanalyze%2Farcpy-mapping%2Flayer-class.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/layer-class.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why not just use&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Ftools%2Fdata-management-toolbox%2Fdelete.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/delete.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;to delete the shapefile since apparently you don't have a lyr file&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 24 Sep 2015 10:00:00 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2015-09-24T10:00:00Z</dc:date>
    <item>
      <title>RemoveLayer - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718405#M55654</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;i try to remove a layer that located in D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp&lt;/P&gt;&lt;P&gt;with this code but i get an error:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os,sys
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
counter = 0
for mxdname in arcpy.ListFiles("*.mxd"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print mxdname # print list of mxd's in the folder
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
&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; if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp":
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'remove' 

&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
del mxd&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this is the error:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; 
environment.mxd
remove

Traceback (most recent call last):
&amp;nbsp; File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\RemoveLayer by data source.py", line 15, in &amp;lt;module&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp":
&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\_base.py", line 78, in _get
&amp;nbsp;&amp;nbsp;&amp;nbsp; (attr_name, self.__class__.__name__))
NameError: The attribute 'dataSource' is not supported on this instance of Layer.
&amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;P&gt;thanks for any help!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:45:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718405#M55654</guid>
      <dc:creator>Yaron_YosefCohen</dc:creator>
      <dc:date>2021-12-12T06:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: RemoveLayer - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718406#M55655</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Read this...&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Fanalyze%2Farcpy-mapping%2Fremovelayer.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/removelayer.htm&lt;/A&gt;&lt;STRONG&gt;​&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then this&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Fanalyze%2Farcpy-mapping%2Flayer-class.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/layer-class.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why not just use&lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Flatest%2Ftools%2Fdata-management-toolbox%2Fdelete.htm" rel="nofollow" target="_blank"&gt;http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/delete.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;to delete the shapefile since apparently you don't have a lyr file&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Sep 2015 10:00:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718406#M55655</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-09-24T10:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: RemoveLayer - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718407#M55656</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your crash is because one of the "Layers" returned from ListLayers() does not support the data source property! As seen in the error messagge &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #f6f6f6;"&gt;NameError: The attribute &lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue; background-color: #f6f6f6;"&gt;'dataSource'&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #006699; background-color: #f6f6f6;"&gt;is&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #006699; background-color: #f6f6f6;"&gt;not&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; supported on this instance of Layer.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A basic reason for this could be because you have "Group" layers setup in your table of contents... these are classed as layers and returned by the ListLayers() function, these obviously have no "DataSource" property, so would cause the crash you see.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Im not sure of the 'proper way' to catch this, but I think this should work (Not tested sorry!):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;arcpy.mapping.RemoveLayer(df, lyr)&lt;/SPAN&gt;
&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'remove'&lt;/SPAN&gt;
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or the full revised script:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os,sys
import arcpy.mapping
from arcpy import env


env.workspace = r"C:\Project"
counter = 0
for mxdname in arcpy.ListFiles("*.mxd"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print mxdname # print list of mxd's in the folder
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
&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; 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; if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp":&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.mapping.RemoveLayer(df, lyr)&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; print 'remove'&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; arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
del mxd&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:45:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718407#M55656</guid>
      <dc:creator>LukeWebb</dc:creator>
      <dc:date>2021-12-12T06:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: RemoveLayer - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718408#M55657</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As an alternative to the try/catch approach you could just check if the layer supports datasource.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
import sys
from arcpy import mapping as m
arcpy.env.workspace = r"C:\Project"
counter = 0
for mxdPath in arcpy.ListFiles("*.mxd"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(mxdPath)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = m.MapDocument(mxdPath)
&amp;nbsp;&amp;nbsp;&amp;nbsp; df&amp;nbsp; = m.ListDataFrames(mxd, "Layers")[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in m.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue
&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; if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m.RemoveLayer(df, lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("Remove")
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="2015-09-24_0647.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/128173_2015-09-24_0647.png" style="width: 620px; height: 672px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:45:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removelayer-arcpy/m-p/718408#M55657</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2021-12-12T06:45:42Z</dc:date>
    </item>
  </channel>
</rss>

