Delete empty Feature Classes from GDB

6253
16
Jump to solution
04-10-2015 07:02 AM
ThomasWhite4
New Contributor II

Hello,

I have many feature classes in multiple gdbs that have empty feature classes. Is there anyway to automate a process to look through each .gdb, find the empty feature classes, and to delete the empty feature classes from the .gdb?

Thank you for your time all!

Regards,

Tom

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

This doesn't include the logic to iterate over the directories, but it satisfies a part of your request to delete the FC of a file geodatabase with zero features.

ws = r'C:\Default.gdb'
arcpy.env.workspace = ws
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
 result = arcpy.GetCount_management(fc)
 countvalue = int(result.getOutput(0))
 if int(countvalue) == 0:
    arcpy.Delete_management(fc)

View solution in original post

16 Replies
ChristianWells
Esri Regular Contributor

Hi Tom,

You could try this out in Python using the GetCount_management() tool.

import arcpy

arcpy.env.workspace = <GDB>

fcList = arcpy.ListFeatureClasses()

for fc in fcList:
     result =  arcpy.GetCount_management(fc)
     count = int(result.getOutput(0))
     if count == 0:
          arcpy.Delete_management(fc)

Get Count (Data Management):

ArcGIS Help (10.2, 10.2.1, and 10.2.2) 

ThomasWhite4
New Contributor II

I am returning the following error for this script:

for fc in fcList:

TypeError: 'NoneType' object is not iterable

Is there a fix for this?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Is there a fix for this?

Yes --- make sure your workspace is actually a workspace and not None.  You might want to post up your complete code so that no one has to guess what is wrong.

0 Kudos
RobinLium
New Contributor

This worked amazingly well in a script I was running where I wanted to delete any outputs from an intersect for-loop.

I added a partial fc name to aid in my scripts efficiency:
fcList = arcpy.ListFeatureClasses("Waters*"

0 Kudos
CarlosBustamante
New Contributor

And when the geodatabase has feature dataset?  What would be the solution?

0 Kudos
JamesCrandall
MVP Frequent Contributor

This doesn't include the logic to iterate over the directories, but it satisfies a part of your request to delete the FC of a file geodatabase with zero features.

ws = r'C:\Default.gdb'
arcpy.env.workspace = ws
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
 result = arcpy.GetCount_management(fc)
 countvalue = int(result.getOutput(0))
 if int(countvalue) == 0:
    arcpy.Delete_management(fc)
ThomasWhite4
New Contributor II

I am returning the following error for this script:

for fc in fcs:

TypeError: 'NoneType' object is not iterable

Is there a fix for this?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Since Christian was really quick with an answer , here's some more that will get you a bit closer to your full requirement.  (sort of tested and only works for file geodatabases):

arcpy.env.workspace = <specify root folder here>
#loop through all folders in the workspace  
for dirpath, dirnames, filenames in arcpy.da.Walk(arcpy.env.workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            ws = dirname
            arcpy.env.workspace = ws
            fcs = arcpy.ListFeatureClasses()
            if not fcs is None:
                for fc in fcs:
                    result = arcpy.GetCount_management(fc)
                    countvalue = int(result.getOutput(0))
                    if int(countvalue) == 0:
                        print str(fc) + " has " + str(countvalue) + " rows features.  deleting...."
                        arcpy.Delete_management(fc)
                        print "deleted"
ThomasWhite4
New Contributor II

Hey James,

This looks like the exact solution that I am looking for. Thank you very much for the assistance. Looks like part of the code is missing on the right side. Do you have a screen shot of the missing part as well?

0 Kudos