When I extract data from my SDE database, I end up with many feature classes that have 0 features in my personal geodatabase. I want to quickly delete these as I have many feature classes within many feature datasets.
I have found scripts but they don't work for me as the feature classes are stored in feature datasets.
Is there a python script available to search all feature datasets within a personal geodatabase to find and delete feature classes that have 0 features?
There is an old thread and I suppose there are more like this with some sample code to do this: https://community.esri.com/message/175591?commentID=175591#comment-175592
Question: any reason you are using a personal GDB?
Randy Burton's suggested post worked. Personal geodatabase works best for my application.
Another suggestion for a possible script in the Python section is: Delete Empty Datasets.
Thanks Randy. This worked!
Randy,
I spoke to soon. As the script ran it gave me an indication that the feature classes with 0 features were getting deleted but in fact they weren't. Back to the search.
Thanks,
Matt
I haven't tested, but what about:
import arcpy
gdb = # path of geodatabase
for root, fds, fcs in arcpy.da.Walk(gdb, datatype="FeatureClass"):
for fc in fcs:
fc = os.path.join(root, fc)
count = int(arcpy.GetCount_management(fc).getOutput(0))
if count:
arcpy.Delete_management(fc)
Thanks Joshua, but unfortunately this didn't work for me.
If you elaborate, maybe I can help more, but I can't do much with "didn't work."
If your geodatabase has both feature classes and feature datasets, I believe you need to deal with each. That is, loop through the datasets and delete feature classes in the dataset and then empty datasets, and follow this by looping through remaining feature classes and deleting the empty ones.
import arcpy
arcpy.env.workspace = r'C:\Path\To\file.gdb'
# check empty datasets
for fds in arcpy.ListDatasets('','Feature'):
print "{}".format(fds)
features = 0
for fc in arcpy.ListFeatureClasses('','',fds):
count = int(arcpy.GetCount_management(fc).getOutput(0))
if count:
features += 1
print "\t{}: {} records".format(fc, count)
else:
print "\t{} records, deleting: {}".format(count, fc)
arcpy.Delete_management(fc)
print "{} has {} remaining features".format(fds, features)
if features == 0:
print "{} dataset is empty, deleting".format(fds)
arcpy.Delete_management(fds)
else:
print "{} has {} remaining features".format(fds, features)
# check empty feature classes
for fc in arcpy.ListFeatureClasses():
count = int(arcpy.GetCount_management(fc).getOutput(0))
if count:
features += 1
print "\t{}: {} records".format(fc, count)
else:
print "\t{} records, deleting: {}".format(count, fc)
arcpy.Delete_management(fc)