I need to remove fields within file GDB and SDE databases that contain only empty or null values. I was hoping to do this for an entire database at a time rather than per feature class. The code below shows the best of what I have so far. It does not, however, accomplish what I had hoped. I am somewhat new to Python and am using ArcMap 10.3.1.
import arcpy
from arcpy import env
env.workspace = r"C:\New Folder\Default.gdb"
def DelFields(path):
env.workspace = path
# first need to point to FCs within DSs
datasets = arcpy.ListDatasets(feature_type='All')
datasets = [''] + datasets if datasets is not None else []
for ds in datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
# list comp syntax:
# [{code}var{code} for var in [list] if var{logic test}].
fieldList = [f.name for f in arcpy.ListFields(fc) if f.type ==
"Short" or "Long" or "Float" or "Double" or "Text" or
"Date" or "Blob" or "Raster" or "Guid"] # "All" did not work
print fieldList # to see what I have so far
if fieldList: # this is a bit of a long shot I think
# The UpdateCursor line gives error:
# TypeError: 'field_names' must be string or non empty sequence of strings
with arcpy.da.UpdateCursor(fc, [fieldList]) as cursor:
for row in cursor:
for i in range (len(fieldList)):
if row[i].rstrip() == "": # trying to find empties
arcpy.DeleteField_management(path, fieldList)
elif row[i] == "<Null>": # trying to find Nulls
arcpy.DeleteField_management(path, fieldList)
cursor.updateRow(row)
DelFields(r"C:\New Folder\Default.gdb")