<?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 delete specific fields in multiple feature classes (not necessarily the same fields)? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1288263#M67625</link>
    <description>&lt;P&gt;It's close, but ran individually won't do what you want. The red squigglies in the pycharm means the variable is not defined. The complete script to run looks like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
import arcpy
import os
from arcpy import env

env.workspace = r"C:\.gdb"
p = arcpy.mp.ArcGISProject(r'C:\project.aprx')
m = p.listMaps('Final')[0]
fcList = m.listLayers()

fcFieldDict = {}

# Create the dictionary of fields from the txt file by splitting the spaces.
with open(r'C:\path to specificdeletefields.txt', 'r') as txt:
    for line in txt:
        # split on two or more spaces
        # \s &amp;lt;- any whitespace
        # {2,} &amp;lt;- cnt to match
        splt = re.compile('\s{2,}')
        vals = splt.split(line)
        try:
            if vals[1]:  # don't append the None's to the list
                if not fcFieldDict.get(vals[0]):
                    fcFieldDict[vals[0]] = [vals[1].strip()]
                else:
                    if vals[1] not in fcFieldDict[vals[0]]:  # avoid appending duplicates
                        fcFieldDict[vals[0]].append(vals[1].strip())

        except:
            arcpy.AddMessage(vals)

# Check the dictionary
for k, v in fcFieldDict.items():
    arcpy.AddMessage(f'fc: {k}, fields to drop:{v}')

# This method is inplace and may take a while to go over each fc
for fc in fcList:
    fcDesc = arcpy.Describe(fc)
    fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
    if fieldsToDrop:
        fcPath = fcDesc.path
        arcpy.DeleteField_management(fcPath, fieldsToDrop)
        arcpy.AddMessage(f'{fc} Dropped {fieldsToDrop}!')
    else:
        arcpy.AddMessage(f'Didnt find matching fc for {fc}')

# This method creates a field map and copies the featureclasses to a new output without the dropped fields
# for fc in fcList:
#     fcDesc = arcpy.Describe(fc)
#     fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
#     if fieldsToDrop:
# 
#         fcPath = fcDesc.path
# 
#         fldMap = arcpy.FieldMappings()
#         # Creating field maps for the two files
#         fldMap.addTable(fc)
# 
#         # Removing unwanted fields
#         for field in fldMap.fields:
#             if not field.required and field in fieldsToDrop:
#                 fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name))
# 
#         arcpy.FeatureClassToFeatureClass_conversion(fcPath, r'path to other.gdb', os.path.basename(fcDesc.name), '', fldMap)
#     else:
#         print(f'Didnt find matching fc for {fc}')&lt;/LI-CODE&gt;&lt;P&gt;Swapping out the method that you want to user (drop in place or export to new fc)&amp;nbsp; The&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fcDesc = arcpy.Describe(fc)
fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
fcPath = fcDesc.path&lt;/LI-CODE&gt;&lt;P&gt;lines will get the featureclass name/path for you by looking at the layer properties, so you wont (shouldn't) have to add the underscores.&lt;/P&gt;&lt;P&gt;You can test it by commenting out the delete at line 42 so it wont actually delete the field. It'll show you at least which layers are not mapped to the featureclass in the gdb.&lt;/P&gt;</description>
    <pubDate>Thu, 11 May 2023 17:16:14 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2023-05-11T17:16:14Z</dc:date>
    <item>
      <title>How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286985#M67597</link>
      <description>&lt;P&gt;I was given this script to modify it to specify certain fields to delete within certain feature classes. How it is now, all fields I put in the list will be deleted in the specified geodatabase. What would I include in this script to further specify that certain fields within a feature class are deleted while certain other fields in another feature class are deleted, etc.? I am a very beginner programmer and intern and my boss gave me this to experiment with and work on, suggesting geonet/ESRI community as a good resource. I've included the script.&lt;/P&gt;&lt;P&gt;Thank you for any help,&lt;/P&gt;&lt;P&gt;Ben&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 20:12:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286985#M67597</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-08T20:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286992#M67598</link>
      <description>&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env
env.workspace = r"Z:\ArcGIS\Projects\Other.gdb"
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps()[0]
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
	dropFields = ['add new filedname here', 'aandefirm','accountid','ACTIVEFLAG','andefirm','appended','As_BuiltDa','ASBUILTDATE','AsBuiltDwg']
	arcpy.DeleteField_management(fc, dropFields)
print (fc + ' Dropped standard fields!')&lt;/LI-CODE&gt;&lt;P&gt;What is the exact logic of how you want it to drop specific fields for specific features?&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 20:33:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286992#M67598</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-05-08T20:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286996#M67599</link>
      <description>&lt;P&gt;I suppose I haven't put too much thought into it. Here is another text file of all of the feature classes on the left side with their particular fields to be deleted on the right side. I've located where all the feature classes are and am going to proceed to import them into the &lt;STRONG&gt;Other.gdb&lt;/STRONG&gt; so that the Python script can operate on them in there for testing.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 20:44:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1286996#M67599</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-08T20:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287005#M67600</link>
      <description>&lt;P&gt;Ah OK, should be pretty simple.&amp;nbsp; Do you have the feature class/table names rather than their aliases?&amp;nbsp; Would make it simpler.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 20:56:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287005#M67600</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-05-08T20:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287007#M67601</link>
      <description>&lt;P&gt;I just need guidance for how to proceed with an optimal way of deleting only the fields tied to the features in the list without deleting all fields in the list for all features. And some of the features have the same fields in them.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 21:03:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287007#M67601</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-08T21:03:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287008#M67602</link>
      <description>&lt;P&gt;Let me see....&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 21:04:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287008#M67602</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-08T21:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287013#M67603</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Traffic_Geonet_eg.PNG" style="width: 409px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/70173i9A1E69FD084327FD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Traffic_Geonet_eg.PNG" alt="Traffic_Geonet_eg.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Like what is seen in the gdb?&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 21:12:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287013#M67603</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-08T21:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287024#M67604</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/635638"&gt;@bcarpenter87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Like what is seen in the gdb?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yes, that is what&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;was getting at regarding the "&lt;SPAN&gt;feature class/table names rather than their aliases." You would need to update that text file, replacing the alias name you have with the actual name in the gdb. If that's not reasonable and those are infact the actual alias names, I suppose you can still do it, there'd just be a few extra steps.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Either way, how I would do it is create a dictionary where the key is the gdb fc name and value is a list (or better yet a set, in case of duplicates) of field names that need to be deleted for that feature class. Then loop through your dictionary items and &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/delete-field.htm" target="_self"&gt;DeleteField&lt;/A&gt; one by one. It'll probably take a while with that many fields. Also keep in mind that you won't be able to delete system generated fields like ObjectID and GlobalID so prepare to handle that.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 21:29:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287024#M67604</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-08T21:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287190#M67605</link>
      <description>&lt;P&gt;I took &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;'s method and converted the list to a dictionary by using a little regex to split on the varying multiple spaces, filtering out the None's (Air Vacuum Valve has one). &amp;nbsp;You can add filters to the dictionary creation to skip the fields such as globalid or objectid if they cannot be deleted and is causing the DeleteFields to fail:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Create the dictionary of fields from the txt file by splitting the spaces.
with open(r'path to\specificdeletefields.txt', 'r') as txt:
    for line in txt:
        # split on two or more spaces
        # \s &amp;lt;- any whitespace
        # {2,} &amp;lt;- cnt to match
        splt = re.compile('\s{2,}')
        vals = splt.split(line)
        try:
            if vals[1]: # don't append the None's to the list
                if not fcFieldDict.get(vals[0]):
                    fcFieldDict[vals[0]] = [vals[1].strip()]
                else:
                    if vals[1] not in fcFieldDict[vals[0]]: #avoid appending duplicates
                        fcFieldDict[vals[0]].append(vals[1].strip())

        except:
            print(vals)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then you have two options- you can delete the fields in the existing database by iterating over the toc lyr names using the listLayers(), getting their path to the underlying dataset from the Describe path, and getting the fields from the dictionary from a lookup:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lyrList = m.listLayers()

for fc in lyrList:
    fcDesc = arcpy.Describe(fc)
    fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
    if fieldsToDrop:
        fcPath = fcDesc.path
        arcpy.DeleteField_management(fcPath, fieldsToDrop)
        print(f'{fc} Dropped {fieldsToDrop}!')
    else:
        print(f'Didnt find matching fc for {fc}')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or do a field map and save the featureclass to another workspace without the fields you want removed:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fcList = m.listLayers()

for fc in fcList:
    fcDesc = arcpy.Describe(fc)
    fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
    if fieldsToDrop:
        
        fcPath = fcDesc.path
    
        fldMap = arcpy.FieldMappings()
        # Creating field maps
        fldMap.addTable(fc.pth)
    
        # Removing unwanted fields
        for field in fldMap.fields:
            if not field.required and field.name in fieldsToDrop:
                fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name))
    
        arcpy.FeatureClassToFeatureClass_conversion(fcPath, r'path to other.gdb', os.path.basename(fcDesc.name), '', fldMap)
    else:
        print(f'Didnt find matching fc for {fc}')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 12:14:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1287190#M67605</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-05-09T12:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1288244#M67622</link>
      <description>&lt;P&gt;Hi Jeff. Thanks for looking at this for me. So I have all of the layers (not all of them will be affected of course, only the ones indicated in the txt) into my 'Experiment.gdb.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the first part of the code you provided in PyCharm just to see what it did. And I think it did what it was supposed to? I've provided an image.&lt;/P&gt;&lt;P&gt;With things set in place, I just needed a few more dots connected for me. How do I run the first part of the your code with either of the second part of the code to get to where I want to be?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see that the layers in the gdb have underscores in them. Will I need to change them as&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;&amp;nbsp;suggested?&amp;nbsp;&amp;nbsp;Do I perform all of this in the Python Window?&lt;/P&gt;&lt;P&gt;Thanks for your time.&lt;/P&gt;&lt;P&gt;Ben&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 16:42:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1288244#M67622</guid>
      <dc:creator>bcarpenter87</dc:creator>
      <dc:date>2023-05-11T16:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: How do I delete specific fields in multiple feature classes (not necessarily the same fields)?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1288263#M67625</link>
      <description>&lt;P&gt;It's close, but ran individually won't do what you want. The red squigglies in the pycharm means the variable is not defined. The complete script to run looks like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
import arcpy
import os
from arcpy import env

env.workspace = r"C:\.gdb"
p = arcpy.mp.ArcGISProject(r'C:\project.aprx')
m = p.listMaps('Final')[0]
fcList = m.listLayers()

fcFieldDict = {}

# Create the dictionary of fields from the txt file by splitting the spaces.
with open(r'C:\path to specificdeletefields.txt', 'r') as txt:
    for line in txt:
        # split on two or more spaces
        # \s &amp;lt;- any whitespace
        # {2,} &amp;lt;- cnt to match
        splt = re.compile('\s{2,}')
        vals = splt.split(line)
        try:
            if vals[1]:  # don't append the None's to the list
                if not fcFieldDict.get(vals[0]):
                    fcFieldDict[vals[0]] = [vals[1].strip()]
                else:
                    if vals[1] not in fcFieldDict[vals[0]]:  # avoid appending duplicates
                        fcFieldDict[vals[0]].append(vals[1].strip())

        except:
            arcpy.AddMessage(vals)

# Check the dictionary
for k, v in fcFieldDict.items():
    arcpy.AddMessage(f'fc: {k}, fields to drop:{v}')

# This method is inplace and may take a while to go over each fc
for fc in fcList:
    fcDesc = arcpy.Describe(fc)
    fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
    if fieldsToDrop:
        fcPath = fcDesc.path
        arcpy.DeleteField_management(fcPath, fieldsToDrop)
        arcpy.AddMessage(f'{fc} Dropped {fieldsToDrop}!')
    else:
        arcpy.AddMessage(f'Didnt find matching fc for {fc}')

# This method creates a field map and copies the featureclasses to a new output without the dropped fields
# for fc in fcList:
#     fcDesc = arcpy.Describe(fc)
#     fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
#     if fieldsToDrop:
# 
#         fcPath = fcDesc.path
# 
#         fldMap = arcpy.FieldMappings()
#         # Creating field maps for the two files
#         fldMap.addTable(fc)
# 
#         # Removing unwanted fields
#         for field in fldMap.fields:
#             if not field.required and field in fieldsToDrop:
#                 fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name))
# 
#         arcpy.FeatureClassToFeatureClass_conversion(fcPath, r'path to other.gdb', os.path.basename(fcDesc.name), '', fldMap)
#     else:
#         print(f'Didnt find matching fc for {fc}')&lt;/LI-CODE&gt;&lt;P&gt;Swapping out the method that you want to user (drop in place or export to new fc)&amp;nbsp; The&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fcDesc = arcpy.Describe(fc)
fieldsToDrop = fcFieldDict.get(fcDesc.nameString)
fcPath = fcDesc.path&lt;/LI-CODE&gt;&lt;P&gt;lines will get the featureclass name/path for you by looking at the layer properties, so you wont (shouldn't) have to add the underscores.&lt;/P&gt;&lt;P&gt;You can test it by commenting out the delete at line 42 so it wont actually delete the field. It'll show you at least which layers are not mapped to the featureclass in the gdb.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 17:16:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-delete-specific-fields-in-multiple/m-p/1288263#M67625</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-05-11T17:16:14Z</dc:date>
    </item>
  </channel>
</rss>

