2

I am trying to delete several fields within a feature class using the .remove function but receive the following error when just testing the remove function in the arcpy module;

ERROR 001334: Cannot delete required field Shape


import arcpy
fc="X:\GIS_PROJECTS\Documentation\Test\UA_GDB_1.gdb\Urban"
fields = arcpy.ListFields("X:\GIS_PROJECTS\Documentation\Test\UA_GDB_1.gdb\Urban")


for field in fields:
    print field.name

FCfields = [f.name for f in arcpy.ListFields(fc)]
DontDeleteFields = ['UA_ID', 'NAME', 'LSAD', 'LSAD_DESC', 'Join_Number', 'DensitySQMI', 'GeoID2', 'F15Growth','Shape', 'F14Growth', 'GrowthAvg', 'ConFinance500', 'ConFinance400', 'ConFinance300']
fields2Delete = list(set(FCfields) - set(DontDeleteFields))
arcpy.DeleteField_management(fc, fields2Delete)
1
  • 4
    Please try not to drastically change the contents your question in response to answers. Rather make an edit and add your refined code as an update with details of what happens. Otherwise it makes the provided answer appear like it's solving a different problem - see @KHibma's answer refers to list.remove() however you have removed all reference to that in your question, so now the first part of the answer is no longer in context.
    – Midavalo
    Apr 13, 2017 at 15:31

2 Answers 2

10

What arcpy.ListFields returns is a python list. When you say list.remove(), you're asking for an item to be removed from the Python list. So for starters, if you want to delete a field from the FC, this won't do what you want. Second, the error you're getting states you can only remove 1 items at a time using the python list.remove() function. You've passed it a list of all your fields.

If you want to remove ALL fields in your FC, you can try the following:

fc = "c:/temp/path2/shp.shp"
for f in arcpy.ListFields(fc):
  if (f.type == 'OID' or f.type == 'Geometry'):
    print("cant delete {}".format(f.name))
  else:
    arcpy.DeleteField_management(fc, f.name)

Or, perhaps a slightly faster way, but requires you know the shape/oid field name:

fields = [f.name for f in arcpy.ListFields(fc)]
print(fields)
>>[u'ObjectID', u'Shape', u'City', u'Country', u'Name', u'Email', u'Manager']
fields.remove('Shape')
fields.remove('ObjectID')
arcpy.DeleteField_management(fc, fields)

One more code snippet - to get a list of all fields. Take a known list you have and remove those items from the field list and then do your delete with the subset list:

FCfields = [f.name for f in arcpy.ListFields(fc)]
DontDeleteFields = ['Shape_STArea__', 'Shape_STLength__', 'STATE', 'FIPS', 'Shape',  'Shape_Length', 'Shape_Area', 'AreaFT']
fields2Delete = list(set(FCfields) - set(DontDeleteFields))
arcpy.DeleteField_management(fc, fields2Delete)
7
  • I am trying to use the python .remove function to delete these fields, and there is actually more fields (around 40 more) that I need to delete that are not just a Geometry type field. Apr 13, 2017 at 14:53
  • Like I said, the remove() function doesnt delete a field. It removes it from the list. The list is what is used as input to the DeleteField tool. Both code snippets read ALL fields in the FC and delete ALL fields (except fields that cannot be deleted: shape and oid)
    – KHibma
    Apr 13, 2017 at 14:55
  • I was then going to take the list with the fields that I would like to have removed and then find a function to delete the fields listed. Delete_Field_management will allow me to call out to this new list wont it? Apr 13, 2017 at 15:00
  • One last update to my answer that will do what I think you want.
    – KHibma
    Apr 13, 2017 at 15:06
  • KHibma now when I try to run the code I get the following error (updated above along with code) saying that it is unable to delete required field Shape. There is no field explicitly named Shape, is it referring to all geometry fields? Apr 13, 2017 at 15:26
2

I think the pythonic way of doing this should be as follows:

for f in arcpy.ListFields(fc):
    if f.name not in set(DontDeleteFields):
        try:
            arcpy.DeleteField_management(fc,f)
        except:
            print arcpy.GetMessages()

This will show an error message if it cannot delete the field but carries on processing. If there is no DontDeleteFields, then commenting out the if line will attempt deleting all fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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