<?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: How do I stop the script from removing layers while it is running? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632669#M74488</link>
    <description>&lt;P&gt;I have this issue as well, never been able to find answer, I have just used addDataFromPath() to re-add the layer.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Jul 2025 23:07:21 GMT</pubDate>
    <dc:creator>2Quiker</dc:creator>
    <dc:date>2025-07-11T23:07:21Z</dc:date>
    <item>
      <title>How do I stop the script from removing layers while it is running?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632576#M74487</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I still need help with my script.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have a Python script to check for duplicates in a key field. The script is removing layers&amp;nbsp; while it is running on successive runs. this is not designed or intended. &lt;/SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;How do I stop the script from removing layers while it is running? &lt;/SPAN&gt;&lt;/STRONG&gt;I have seen other posts on Arcypy removing layers when scripts run, but there is no definitive answer or way to stop it. I need to stop it from removing features while it is running.&lt;/P&gt;&lt;P&gt;My script is:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# F is the list of feature layers that the script is to iterate over
F=("Sanitary Sewer Manholes","Water Network Structures","Water Fittings","Water System Valves","sw STRUCTURES", "Water Service Connections","Water Hydrants","Sewer Gravity Mains","Sewer Pressurized Mains","Storm Drain Pipes", "Water Lateral Lines", "Water Mains")
# output path for MBID duplactes
m="C:\\Users\\lconner\\Documents\\ArcGIS\\Projects\\edit_map7\\edit_map7.gdb\\MBID_duplicate" + str(1)


for e in F:
    # test if the feature class is in the map. if so clear the slection and run FindIdentical.
    if arcpy.Exists(e)== True:
        try:
            s=str(e).replace(" ", "_")
            o= "C:\\Users\\lconner\\Documents\\ArcGIS\\Projects\\edit_map7\\edit_map7.gdb\\" +s+"_findidencal"+("_a")

            # clear slection, so the finde identical runs on all features 
            arcpy.management.SelectLayerByAttribute(
            in_layer_or_view=e,
            selection_type="CLEAR_SELECTION",
            where_clause="",
            invert_where_clause=None
            )

            arcpy.management.FindIdentical(
            in_dataset= e,
            out_dataset=o,
            fields="FACILITYID",
            xy_tolerance=None,
            z_tolerance=0,
            output_record_option="ONLY_DUPLICATES")

            count_result = arcpy.management.GetCount(o)
            count = int(count_result[0])
            
            #print out to check if the findidentical ran
            print('successfuly ran find indentical for ' + str(e))  

        # handle an exeption if the code did not run
        except Exception as err:
            print(f"FindIdentical failed for {e}: {err}")
            continue
   
    #flag if the the frature is not in the map at this stage e.g. to begin with
    elif (arcpy.Exists(e)== False):
        print(str(e)+ " is not in the document 1") 

    
    try:
        # if there are duplactes join the duplicate out put table to the feature class and slect the duplactes 
        if (count &amp;gt; 0) and (arcpy.Exists(e)== True):
            # print statments to flag duplicates 
            print("Duplicate entries in "+str (e) )
            print(f"Number of duplicates found: {count}")

            #remove all joins to rule out previsous existing joins ant causing problems 
            arcpy.management.RemoveJoin(
                in_layer_or_view=e,
                join_name=""
                )

            arcpy.management.AddJoin(
                in_layer_or_view=e,
                in_field="OBJECTID",
                join_table=o,
                join_field="IN_FID",
                join_type="KEEP_ALL",
                index_join_fields="NO_INDEX_JOIN_FIELDS",
                rebuild_index="NO_REBUILD_INDEX",
                join_operation="JOIN_ONE_TO_FIRST"
                )

            arcpy.management.SelectLayerByAttribute(
                in_layer_or_view= e,
                selection_type="NEW_SELECTION",
                where_clause="IN_FID IS NOT NULL",
                invert_where_clause=None
                )

        # print statment for when there are no duplacts and the  FC is in the map
        elif (count == 0) and (arcpy.Exists(e)== True):
            print("no Duplicate in " + str(e) )

        # pirnt statment if the FC is not in the doucment at the time of preforming the join
        elif arcpy.Exists(e) == False:
            print(str(e) + " is not in the document 2" )
        
    #exeption if the FC is in the map byt the join cant be preformed
    except Exception as err:
            print(f"AddJoin failed for {e}: {err}")
            continue

    # render a blank line for neat print formating
    print("    ")
   
    
#portion of the script dealing with checking duplicat MBIDs
# if Water Service Connections is in the map findidentical
if arcpy.Exists("Water Service Connections")== True:
    try:
        arcpy.management.SelectLayerByAttribute(
            in_layer_or_view="Water Service Connections",
            selection_type="NEW_SELECTION",
            where_clause="MBID IS NOT NULL",
            invert_where_clause=None
            )

        arcpy.management.FindIdentical(
            in_dataset="Water Service Connections",
            out_dataset=m,
            fields="MBID",
            xy_tolerance=None,
            z_tolerance=0,
            output_record_option="ONLY_DUPLICATES"
            )
   
    #execption for handling erors where the FC is in the map
    except Exception as err:
            print(f"FindIdentical failed for Water Service Connections: {err}")
            
    # join and select if there are duplicates 
    if count &amp;gt; 0:
        print("Duplicate MBID in Meters " )
        print(f"Number of duplicates found: {count}")
        try:
            arcpy.management.AddJoin(
                in_layer_or_view="Water Service Connections",
                in_field="OBJECTID",
                join_table=m,
                join_field="IN_FID",
                join_type="KEEP_ALL",
                index_join_fields="NO_INDEX_JOIN_FIELDS",
                rebuild_index="NO_REBUILD_INDEX",
                join_operation="JOIN_ONE_TO_FIRST"
                )


            arcpy.management.SelectLayerByAttribute(
                in_layer_or_view= "Water Service Connections",
                selection_type="NEW_SELECTION",
                where_clause="IN_FID IS NOT NULL",
                invert_where_clause=None
                )
        #expection if the join could not be preformed
        except:
            print("the join for the duplacate MBIDs could not be preformed on Water Service Connections")        
    # print stament if the are no duplicates 
    else:
            print("no Duplicate MBIDs ")

#indicate the scipt is finshed 
print("done")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In particular, it works fine on the 1st run. However running it a2nd time it removes the feature classes it found duplicates in. The layers are being removed after the find identical feature.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the out puts for each run&lt;/P&gt;&lt;P&gt;1st run:&lt;/P&gt;&lt;P&gt;successfuly ran find indentical for Sanitary Sewer Manholes&lt;BR /&gt;no Duplicate in Sanitary Sewer Manholes&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Network Structures&lt;BR /&gt;no Duplicate in Water Network Structures&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Fittings&lt;BR /&gt;no Duplicate in Water Fittings&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water System Valves&lt;BR /&gt;no Duplicate in Water System Valves&lt;BR /&gt;&lt;BR /&gt;sw STRUCTURES is not in the document 1&lt;BR /&gt;sw STRUCTURES is not in the document 2&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Service Connections&lt;BR /&gt;no Duplicate in Water Service Connections&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Hydrants&lt;BR /&gt;no Duplicate in Water Hydrants&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Sewer Gravity Mains&lt;BR /&gt;no Duplicate in Sewer Gravity Mains&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Sewer Pressurized Mains&lt;BR /&gt;no Duplicate in Sewer Pressurized Mains&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Storm Drain Pipes&lt;BR /&gt;Duplicate entries in Storm Drain Pipes&lt;BR /&gt;Number of duplicates found: 9&lt;BR /&gt;&lt;BR /&gt;Water Lateral Lines is not in the document 1&lt;BR /&gt;Water Lateral Lines is not in the document 2&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Mains&lt;BR /&gt;Duplicate entries in Water Mains&lt;BR /&gt;Number of duplicates found: 2&lt;BR /&gt;&lt;BR /&gt;Duplicate MBID in Meters&lt;BR /&gt;Number of duplicates found: 2&lt;BR /&gt;done&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;on the 2nd run:&lt;/P&gt;&lt;P&gt;successfuly ran find indentical for Sanitary Sewer Manholes&lt;BR /&gt;no Duplicate in Sanitary Sewer Manholes&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Network Structures&lt;BR /&gt;no Duplicate in Water Network Structures&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Fittings&lt;BR /&gt;no Duplicate in Water Fittings&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water System Valves&lt;BR /&gt;no Duplicate in Water System Valves&lt;BR /&gt;&lt;BR /&gt;sw STRUCTURES is not in the document 1&lt;BR /&gt;sw STRUCTURES is not in the document 2&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Service Connections&lt;BR /&gt;no Duplicate in Water Service Connections&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Hydrants&lt;BR /&gt;no Duplicate in Water Hydrants&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Sewer Gravity Mains&lt;BR /&gt;no Duplicate in Sewer Gravity Mains&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Sewer Pressurized Mains&lt;BR /&gt;no Duplicate in Sewer Pressurized Mains&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Storm Drain Pipes&lt;BR /&gt;Storm Drain Pipes is not in the document 2&lt;BR /&gt;&lt;BR /&gt;Water Lateral Lines is not in the document 1&lt;BR /&gt;Water Lateral Lines is not in the document 2&lt;BR /&gt;&lt;BR /&gt;successfuly ran find indentical for Water Mains&lt;BR /&gt;Water Mains is not in the document 2&lt;BR /&gt;&lt;BR /&gt;Duplicate MBID in Meters&lt;BR /&gt;Number of duplicates found: 2&lt;BR /&gt;done&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;Laura&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 19:19:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632576#M74487</guid>
      <dc:creator>Laura_m_Conner</dc:creator>
      <dc:date>2025-07-11T19:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I stop the script from removing layers while it is running?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632669#M74488</link>
      <description>&lt;P&gt;I have this issue as well, never been able to find answer, I have just used addDataFromPath() to re-add the layer.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 23:07:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632669#M74488</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2025-07-11T23:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I stop the script from removing layers while it is running?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632684#M74489</link>
      <description>&lt;P&gt;I think this is default behaviour, you can alter the behaviour of tools through the options dialog, try unticking this option to see if this creates the behaviour you are after?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DuncanHornby_0-1752334830155.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/136447iBECEEC58FC9DBF8B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DuncanHornby_0-1752334830155.png" alt="DuncanHornby_0-1752334830155.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jul 2025 15:40:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632684#M74489</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2025-07-12T15:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I stop the script from removing layers while it is running?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632847#M74492</link>
      <description>&lt;P&gt;I will have to try unchecking the feature. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Jul 2025 14:51:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-stop-the-script-from-removing-layers/m-p/1632847#M74492</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2025-07-14T14:51:24Z</dc:date>
    </item>
  </channel>
</rss>

