Delete Empty Datasets

2124
6
Jump to solution
06-05-2018 08:04 AM
SarojThapa1
Occasional Contributor III

I am working on creating a script/tool that deletes empty feature classes that reside inside feature datasets. Some datasets have empty feature classes. When those empty feature classes are deleted, some datasets become empty. I could not figure out how to count the number of datasets in a file gdb and delete them if they are empty.

 

import arcpy
import os
arcpy.env.workspace = r"C:\Users\s\Documents\ArcGIS\Default.gdb"
#arcpy.env.workspace = arcpy.GetParameterAsText(0)
#listFCs = arcpy.ListFeatureClasses()
listDSs = arcpy.ListDatasets("*", "All")
# Delete empty Feature Classes inside Feature Dataset
for ds in listDSs:
 for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
 count = str(arcpy.GetCount_management(fc))
 if count == "0":
 arcpy.Delete_management(fc)
# Delete empty Feature Datasets
# First count number of feature datasets and check if they are empty
for ds2 in listDSs:
 print (ds2)
print "Script Completed!"

 I used similar approach to fc in ds, but got the following errors:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Table View.
ERROR 000840: The value is not a Raster Layer.
Failed to execute (GetCount).

Please help.

Thanks,

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

I haven't tested it, but try replacing line 16 in the previous code with an if/else to check count of features:

import arcpy

arcpy.env.workspace = r'C:\Path\To\File.gdb'

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) # uncomment to delete fc
    # line 16: 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) # previous line 16

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

The indentation is off with the code, but I will assume that is just an artifact of copying and pasting and not in your code itself.

The error is telling you that Get Count is having the issue.  The error messages imply that you are trying to get the count of data that doesn't support counting.  You will need to provide some more information about the types of layers in your GDB for any specific suggestions.

0 Kudos
RandyBurton
MVP Alum

You might try something like:

import arcpy

arcpy.env.workspace = r'C:\Path\To\File.gdb'

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) # uncomment to delete fc
    print "{} has {} remaining features".format(fds, features)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
SarojThapa1
Occasional Contributor III

Thank you Randy for your neat and clean codes. After deleting empty feature classes, say if a feature dataset has 2 empty features, then that feature dataset is empty now. I want to delete those empty feature datasets too. Can you please help me with this?

Thanks,      

0 Kudos
RandyBurton
MVP Alum

I haven't tested it, but try replacing line 16 in the previous code with an if/else to check count of features:

import arcpy

arcpy.env.workspace = r'C:\Path\To\File.gdb'

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) # uncomment to delete fc
    # line 16: 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) # previous line 16
SarojThapa1
Occasional Contributor III

Thank you so much Randy. It works great. 

CatherineHall1
New Contributor

Definitely worked. Thank you!

0 Kudos