Removing Empty Feature Classes with Python
The Problem: You have a geodatabase with a lot of empty feature classes
This often happens if the geodatabase has been created from a template of standard themes, but this particular version doesn’t contain information to stick into all the available slots. It can be difficult to get a handle on what’s actually there with all the empty feature classes clogging up space. It can take up extra processing time or cause errors depending on what you’re doing with the data later. And, it’s just bad organization. The best practice is to get rid of them. You can do so by moving down the feature class list in ArcCatalog, checking for empties and deleting them one by one. But with large databases (the kind that need trimming down the most) this can take a lot of time.
The slow manual method:

The fast automated method: use this python code that an associate shared with me!
Copy and paste the following into any text editor and save it as a python .py file.
##############################################################
## Delete Empty Feature Classes from Multiple Geodatabases
##
## This script loops through all geodatabases within a directory
## and removes empty featureclasses from each geodatabase.
##
## User must hardcode directory. Run this script in IDLE.
##
## Python Version 2.4
##############################################################
# Create the Geoprocessor object
import arcgisscripting, os, sys, string
gp = arcgisscripting.create()
# Load required toolboxes...
gp.AddToolbox("C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx")
# Set workspace
gp.workspace = "C:\\Documents and Settings\\user\\Desktop\\gis\\"
# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces("*", "access")
mdb = listmdb.next()
# loop through the personal geodatabases
while mdb:
print mdb
gp.workspace = mdb
# list feature datasets in personal geodatabase
listfdset=gp.listdatasets()
fdset=listfdset.next()
if not fdset == None:
gp.workspace = mdb + "\\" + fdset
# list feature classes in the personal geodatabase
fcs = gp.listfeatureclasses()
fcs.reset()
fc = fcs.next()
# loop through the featureclasses
while fc:
# print fc
# if the feature class table has no records, delete the featureclass
if gp.GetCount_management(fc) == 0:
gp.Delete_management(fc)
print fc + "deleted from" + mdb
fc = fcs.next()
mdb = listmdb.next()
The only thing you will need to change is the directory under # Set workspace. Put the path to your geodatabase(s) in between the gp.workspace = “” quotes, remembering to use two slashes \\ in between folder names.
This works with personal geodatabases. If you are using file geodatabases, go to the next line under # list personal geodatabases… and replace “access” with “FileGBD” between the gp.listworkspaces(“*”, “”) quotes.
Now, start Python IDLE, which ships with the ArcGIS suite. Open up your .py file. Then, select Run –> Run Module (or hit F5). This is what you will see:
“
“
It may take some time to run, but that is time you can spend doing other things. When it’s done, those empty feature classes are history.
You can edit, save and rerun the .py file as many times as you need if your geodatabases are spread between different directories. (Or, for even more automation, copy them all into the same directory before you start, and let the script loop through them).
Python is an extremely handy tool. If you are interested in learning more about it, see the resources below.
- HowTo: Get started writing Python geoprocessing scripts in ArcGIS 9x (ESRI Support)
- A Guide to the Python Universe for ESRI Users (PDF File)
Tags: attribute table, automation, geodatabase, python
September 28th, 2009 at 10:01 am
I tried your very useful script, but had to correct it a little bit. It cleaned only the first Feature DataSet, so I added an additional while-loop:
##############################################################
## Delete Empty Feature Classes from Multiple Geodatabases
##
## This script loops through all geodatabases within a directory
## and removes empty featureclasses from each geodatabase.
##
## User must hardcode directory. Run this script in IDLE.
##
## Python Version 2.4
##############################################################
# Create the Geoprocessor object
import arcgisscripting, os, sys, string
gp = arcgisscripting.create()
# Load required toolboxes…
gp.AddToolbox(“C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx”)
# Set workspace
gp.workspace = “C:\\tmp\\pgdb\\”
# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces(“*”, “access”)
##listmdb = gp.listworkspaces(“*”, “FileGBD”)
mdb = listmdb.next()
# loop through the personal geodatabases
while mdb:
print mdb
gp.workspace = mdb
# list feature datasets in personal geodatabase
listfdset=gp.listdatasets()
fdset=listfdset.next()
# loop through feature dataset
while fdset:
#print “fdset: “, fdset
if not fdset == None:
gp.workspace = mdb + “\\” + fdset
# list feature classes in the personal geodatabase
fcs = gp.listfeatureclasses()
fcs.reset()
fc = fcs.next()
# loop through the featureclasses
while fc:
#print fc, gp.GetCount_management(fc)
# if the feature class table has no records, delete the featureclass
if gp.GetCount_management(fc) == 0:
gp.Delete_management(fc)
print fc + “deleted from ” + mdb
fc = fcs.next()
fdset=listfdset.next()
mdb = listmdb.next()
October 4th, 2009 at 2:31 pm
Thanks for the improved script, Martin! I didn’t notice this because I’ve only used it with geodatabases that weren’t organized into feature datasets.
February 15th, 2010 at 11:21 pm
I’m having trouble with Martin’s amended script.
I am no programmer… but I have had success with the original script provided, however I also wanted to remove feature datasets from my geodatabase.
When I run Martin’s amended script (having changed my geodatabase workspace) the result is that my script appears to get stuck in the while loop, as the window shows my geodatabase workspace location repeating over and over and over again.
Any suggestions?
June 3rd, 2010 at 7:17 am
Oh, crap…i think somebody can help with the indentation stuff. i’m not a web pro when it comes to page styling.
or u can try this:
at
while.mdb:
next line 1 tab, upto
while fdset:
nextline 2 tabs, upto
while fc:
nextline 3 tabs,till fc=fcs.next()
then one tab back at fdset=listfdset.next()
one more tab back at mdb=listmdb.next()
additional:
gp.workspace = mdb + “” + fdset starts with 3 tabs after
if not fdset == None:
both the lines:
gp.Delete_management(fc)
print fc + ” deleted from” + mdb
starts with 4 tabs
sorry for the confusion, but it works. all the tabs are from the first column, except the coming back ones.
Happy automating!!!