Removing empty feature classes from a geodatabase

5662
5
02-10-2011 04:15 PM
JustinOdell
Occasional Contributor III
Does anybody know of a script that exists to do this?

I have come across this link where some people have had success, others not.

I tried it myself, but no luck.
Tags (2)
0 Kudos
5 Replies
StefanHaglund1
Esri Contributor
Hi Hydronaut,

What specific problem do you have with the code in the link?

I believe that with some of the changes suggested in the comments in the link you sent, this should work. I was removing empty layers from mxd�??s with a similar method. What I found (in v.10) is that you need to make an integer of the results object from GetCount_management.

So if you look at comment #5 in the link, which says:

result = arcpy.GetCount_management(fc)
print result
if result == 0:
print fc + �?� is empty.�?�
arcpy.delete_management(fc)


Change that to
result =  arcpy.GetCount_management(lyr)
count = int(result.getOutput(0))
if count == 0:
 arcpy.mapping.RemoveLayer(df, lyr)
 print "Removing " + lyr
else:
 print "Keeping " + lyr


In the code above, substitute arcpy.mapping.RemoveLayer for arcpy.Delete_management.

Also, you might need to handle FC's inside feature datasets like the first comment suggests.
0 Kudos
StefanHaglund1
Esri Contributor
Adding to my post above, there's an explanation in the help why you need to handle the result object.

Have a look here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000n000000.htm

In the note under the "Getting results from a tool" heading, it says:
The result object's getOutput method returns a Unicode string from result objects that have output values. This is an important consideration when running a tool such as GetCount, which provides a count of records in a table, or CalculateDefaultClusterTolerance, which provides a cluster tolerance value. To convert the value to the expected type, it will have to be converted from a unicode string using built-in Python functions such as int() or float().
0 Kudos
JustinOdell
Occasional Contributor III
This is the code I have.  I've never taken a Python scripting course, so I'm a bit of a noob when it comes to programming.

The problem I find when running the script in IDLE, is that it gets stuck in the while loop, and (I think) tries to keep removing the same feature dataset.

##############################################################
## 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 = "L:\\Justin\\00_jobs\\Python_Scripting_Test\\"

# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces("*", "FileGDB")
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()


Any help is greatly appreciated.
0 Kudos
NiklasNorrthon
Occasional Contributor III
My advice is: Don't loop with while! (Not in arcpy, and not in previous versions of arcgis either). The ArcGIS sample code is written by someone not knowing python.

arcpy version:
listmdb = arcpy.ListWorkspaces('*', 'FileGDB')
for mdb in listmdb:
    print mdb
    # same logic as above
    # change inner loops to for loops too
    # don't call next, since it's done behind the scene with the for loop


ArcGIS 9.2 version:
listmdb = gp.ListWorkspaces('*', 'FileGDB')
for mdb in iter(listmdb.next, None): # Note: no function parantheses following next
    print mdb
    # same logic as above
    # change inner loops to for loops too
    # don't call next, since it's done behind the scene with the for loop
0 Kudos
JustinOdell
Occasional Contributor III
Hmm... I seem to be making less progress than with the while loops.  I will get myself in to do a Python course, but as it stands I am struggling to get the code to do what I want it to. 

Is anybody able to write or test the code and fix it so that it deletes empty feature classes AND empty feature datasets from a geodatabase?  I would be very thankful.
0 Kudos