Add Functionality to Delete a Field (if it exists)

1487
3
01-04-2019 01:52 PM
Status: Closed
Labels (1)
DevonBurton3
New Contributor II

Currently with ArcPro the delete field tool in data management will generate a warning if a field doesn't exist in the input and error out the task. Now this might be a bug, as I don't think warnings are supposed to stop an iterating script dead in its tracks, but what if you made the code like so:

inFC = arcpy.GetParameterAsText(0)

ListOfInputFields = arcpy.GetParameterAsText(1)

ListOfInputFields = ListOfInputFields.split(";")

for delField in ListOfInputFields: 

    fldLst = arcpy.ListFields(inFC) 

    fldLst2 = []

    for fld in fldLst:

        fldLst2.append(fld.baseName)

    if delField is in fldLst:

        arcpy.DeleteField_management(inputFC,delField)

    else:

        arcpy.AddMessage(deleteField + " was not found in " + inputFC)

        continue

Anyways just an idea. It's probably not the best way of writing out the code but maybe you get the point?

3 Comments
KoryKramer

It seems like the request would be specific to scripting?  But you can already write checks like this into a script...

So is the request to bake an 'if exists' check right into the Delete Field—Data Management toolbox | ArcGIS Desktop tool?

DevonBurton3

Yes, as a parameter. But I can also get that it's better to just write that check into my own scripts at the same time. It'd likely cause confusion though as an optional parameter. Not a good enough reason to change arcpy.DeleteField_management, I suppose. 

MargaretCrawford
Status changed to: Closed

Delete Field when run in a script with a field that does not exist will simply raise a warning and will not stop execution of the script. You can add extra logic as indicated above if you wish to do other processing steps if the Delete Field raises the warning.