python script available to search all feature datasets within a personal geodatabase to find and delete feature classes that have 0 features

1541
9
01-10-2019 11:37 AM
MatthewGregory
New Contributor

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?

0 Kudos
9 Replies
XanderBakker
Esri Esteemed Contributor

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? 

MatthewGregory
New Contributor

Randy Burton's suggested post worked. Personal geodatabase works best for my application.

0 Kudos
RandyBurton
MVP Alum

Another suggestion for a possible script in the Python section is: Delete Empty Datasets.

MatthewGregory
New Contributor

Thanks Randy. This worked!

0 Kudos
MatthewGregory
New Contributor

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

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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)
MatthewGregory
New Contributor

Thanks Joshua, but unfortunately this didn't work for me.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

 If you elaborate, maybe I can help more, but I can't do much with "didn't work."

0 Kudos
RandyBurton
MVP Alum

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)