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
Solved! Go to Solution.
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)
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):
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?
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.
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*")
And when the geodatabase has feature dataset? What would be the solution?
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)
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?
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"
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?