My question is similar to that I asked here. The script below, however, is intended to delete unnecessary fields from a single standalone table rather than a database full of tables. The answer in the question referenced above has not performed consistently and I am thinking narrowing it down to a single table may provide some insight to why this is so.

import arcpy
from arcpy import env

table_name = (r'C:\MyDatabase.gdb\StandAloneTable')
field_names = [f.name for f in arcpy.ListFields(table_name) if not f.required]
keep = True

with arcpy.da.SearchCursor(table_name, field_names) as cursor:  
    for row in cursor:
        v = row[0]
        if v is None or isinstance(v, basestring) and \
           (v == '<Null>' or \
            v == 0 or \
            v == '' or \
            v == ' ' or \
            v == '  '):
            keep = False

    if keep is False:
        arcpy.DeleteField_management(table_name, field_names)

This script runs without error aside from failing to delete the fields populated only with the values identified in first if-statement as I had intended. No fields are being deleted. Any ideas on what I am doing wrong?

Also, while I am testing this script on standalone tables within a file GDB, the standalone tables I plan to eventually run this script for are housed within an ArcSDE geodatabase.

1 Answer 1


I think the reason nothing is being deleted may be because you break the with and you're not even hitting your if keep is False:

You may want to outdent (un-indent) your if block.

Note: That said, I believe that if you change this if you will delete ALL fields in your field_names as you are not restricting to a single field or a specific list of fields to delete. arcpy.DeleteField_management(table_name, field_names) will delete all fields in field_names and this list hasn't been updated.

The answer in your linked question loops through the fields, and restricts the deletion to just the current field.

for field in field_names:

Doing a quick edit on your linked answer, this should work (although I'm not a fan of setting keep = False first as this runs the risk of deleting a field if something weird happens and the if block doesn't find a valid value):

import os
import arcpy
arcpy.env.workspace = r'C:\MyDatabase.gdb'
table_name = (r'StandAloneTable')
field_names = [f.name for f in arcpy.ListFields(table_name) if not f.required]

# Iterate over the fields
for field in field_names:
    # Set a test variable to False
    keep = False
    # Iterate over each feature; if it has a non-null value, then set
    #   the test variable to True and then break out of the cursor
    with arcpy.da.SearchCursor(table_name, field) as cursor:
        for row in cursor:
            v = row[0]
            if v is not None or \
              (isinstance(v, basestring) \
              and (v.lower.strip() != ''\
                or v != '<null>')):
                keep = True

    # If the test variable was unchanged, delete the field
    if keep is False:
        arcpy.DeleteField_management(fc, field)
  • I bet that's it. I'd probably set keep to true again too or it will continue to be false and just delete everything after the first time it gets set to false? Feb 10, 2017 at 15:47
  • @jbchurchill yes (after re-reading your comment) I agree, keep needs to be re-set to True for each loop
    – Midavalo
    Feb 10, 2017 at 15:50
  • @jbch @Mid I have tried combinations of these recommendations (outdenting if block, removing break, workspace of a single table), and while sometimes I get deleted fields, the expected fields are not being deleted. With the above script, if all cells in field have, for example <Null>, except one, shouldn't keep = True? And shouldn't v != 0 result in keep = false?
    – reevesii
    Feb 10, 2017 at 18:16
  • @reevesii that is correct, that is what the if statement above says - if one of the cells is not NULL then keep = True. v is not None means if a value is found that isn't NULL, i.e. it contains a value.
    – Midavalo
    Feb 10, 2017 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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