I have some shapefiles and each shapefile is having some blank or zero value fields. My task is to delete blank/zero value fields for all shapefiles in the folder. I have searched for solution in this site and found one Python program but unfortunately I am not successful with available Python program. Below is the my code and I don't know where I made a mistake.

I have attached image also for reference and the fields i wanted to delete are shown with line

After running code, I did not encounter any error message but fields are not removed


Empty field deleted
import os
import arcpy
from arcpy import env
path = r"C:\SOFTWARE\Shapefiles\test"
env.workspace = path
fcs = arcpy.ListFeatureClasses()  

# Iterate over the feature classes
for fc in fcs:
    # Test whether the field is required--rather than testing the field type
    fieldList = arcpy.arcpy.ListFields(fc)

    # Iterate over the fields
    for field in fieldList:
        # Set a test variable to False
        with arcpy.da.SearchCursor(fc, field.name) as cursor:
            for row in cursor:`enter code here`
                if field == " " or field == 0:
                    arcpy.DeleteField_management(fc, field.name)
print ("Empty field deleted")

enter image description here

  • 2
    The definition of a blank field in dBase is subject to debate, since .dbf predates numeric or string nulls.
    – Vince
    Jan 12, 2019 at 19:59

3 Answers 3


I'm not currently able to test this but here are some issues I can see:

You need to loop through every record in every field to check that they are all blank or 0. Once you have looped through these you can then delete the field.

You should create a list of fields to delete (once looped to verify) and then use DeleteField() only once after the cursor is complete. This would allow for 2 things, 1) reduce potential for the cursor to break if you delete a field, and 2) you can the output the list of fields rather than actually deleting to help test/debug your script

Something like this (note this is untested):

for fc in fcs:
    fieldList = arcpy.arcpy.ListFields(fc)
    fieldsToDelete = list() #Create a list of fields to delete at the end

    for field in fieldList:
        deleteField = False #Only delete fields with deleteField = True
        valCount = 0 #Count the number of "non-blank" values
        with arcpy.da.SearchCursor(fc, field.name) as cursor:
            for row in cursor:
                if row[0] is None or row[0] in ["", " ", 0]:
                    valCount += 0
                    valCount =+ 1
                    break #No need to continue if a value is found
                if valCount == 0:
                    deleteField = True #If no values found, mark the field for deletion

        if deleteField == True:
            fieldsToDelete.append(field.name) #Add the field name to list of fields to delete

    print fieldsToDelete #Output the list of fields that will be deleted
    # Uncomment the line below once you are happy the correct fields will be deleted
    # arcpy.DeleteField_management(fc, fieldsToDelete) #Delete blank fields

Here is one approach that uses a generator to identify delete fields:

import arcpy

arcpy.env.workspace = r'C:\temp\test.gdb'

for fc in arcpy.ListFeatureClasses():
    fields = arcpy.ListFields(fc)
    for field in fields:
        vals = set(row[0] for row in arcpy.da.SearchCursor(fc, field.name)) # Generate a unique list of values in field
        if any(map(lambda each: each in vals, [0, '0',' ', '', None])) and len(vals) == 1: # Check if field is of length = 1 and contains search characters
            arcpy.DeleteField_management(fc, field.name)


enter image description here


enter image description here


Create a list of fields per feature class. Next iterate through each table and check each attribute for value. If an attribute has a value add the field to a list of keep field. Any field that doesn't appear in the keep field list can be deleted.

import arcpy
from arcpy import env
path = r"C:\SOFTWARE\Shapefiles\test"
env.workspace = path
fcs = arcpy.ListFeatureClasses()  

# Iterate over the feature classes
for fc in fcs:
    print fc
    print "listing fields"
    flds = [f.name for f in arcpy.ListFields (fc)]
    print "iterating table"
    #set of fields to keep
    keepFlds = set ()
    #number of fields
    fldCount = len (flds)
    with arcpy.da.SearchCursor (fc, flds) as curs:
        for row in curs:
            for i in range (fldCount):
                #check if field has value
                if not row [i] in ("", 0, None):
                    keepFlds.add (flds [i])

    #delete fields
    for fld in flds:
        if not fld in keepFlds:
            print "deleting field", fld
            arcpy.DeleteField_management (fc, fld)
  • Hi @Emil Brundage.Thank you for sharing code. With your code I can delete all fields with value "0" deleting but can't delete empty fields(nothing in that) without any attributes. Task1. delete all fields with zero value (it is working with your code) 2.Also I want to delete other empty fields without attributes. Can you help me to fulfill my task
    – Arb
    Jan 24, 2019 at 15:00
  • This code should work in either case. Maybe your field has a single space? " " Jan 24, 2019 at 16:06
  • Hi Emil Brundage, as per your suggestion, I have checked fields for spaces. They are empty fields without any attributes even no spaces. More ever I have added some empty fields without any spaces and run code but empty fields are not deleted and they are remains same. Any suggestion/idea regarding code?
    – Arb
    Jan 26, 2019 at 14:56
  • I’ve tweaked the code a bit @Arb . See how that does. Jan 26, 2019 at 19:16
  • Oh it is working now. small issue is code deleting fields only for the first shape file and not deleting fields for further shape files in the same folder. Thank you.
    – Arb
    Jan 27, 2019 at 16:26

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.