Select to view content in your preferred language

how to delete multiple mdb or gdb empty feature class.

1331
6
04-03-2018 06:36 AM
santhoshp
Occasional Contributor

Dear Friends,

Kindly find the attached multiple mdb's. how to delete multiple mdb or gdb empty features class.any code available please help me.

Thanks

Santhosh

Tags (2)
0 Kudos
6 Replies
JamesCrandall
MVP Frequent Contributor

http://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm

arcpy.env.workspace = <workspace>
fcs = arcpy.ListFeatureClasses('*')
for fc in fcs:
    test = int(arcpy.GetCount_management(fc).getOutput(0))
    print 'Feature Count: {}'.format(test)
    if test > 0:
        print 'Deleting Feature Class: {}'.format(fc)
        arcpy.Delete_management(fc)
MitchHolley1
MVP Regular Contributor

To add onto the concepts covered by @James, the following will take an input directory and loop over the gdbs and FCs for each one. 

import arcpy
import os

inputDir = r'path\to\directory\that\houses\geodatabases'
ext = ['.mdb','.gdb']
gdbs = [x for x in os.listdir(inputDir) if x[-4:] in ext]

for g in gdbs:
    fullpath = os.path.join(inputDir, g)
    arcpy.env.workspace = fullpath
    for fc in arcpy.ListFeatureClasses():
        rowCount = arcpy.GetCount_management(fc)[0]
        if rowCount == '0':
            print "Deleting empty FC: {0}".format(os.path.join(g,fc))
            arcpy.Delete_management(os.path.join(fullpath,fc))
        else:
            print "{0} contains {1} records".format(os.path.join(g,fc), rowCount)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
santhoshp
Occasional Contributor

Hi Dear,

attached below snapshot can you give the solution for attached image.Delete multiple mdb empty feature class one time all mdb.

delete Multiple mdb or gdb empty feature class one time

Thanks

Santhosh

0 Kudos
RandyBurton
MVP Alum

You are using filename in the listing of extensions.  Just use the extension:

ext = ['.mdb','.gdb']

If the file ends in either of these extensions, then the filename will be found.  Also, you need to set the inputDir to the correct value.

MitchHolley1
MVP Regular Contributor

Change the inputDir path to the directory that houses your geodatabases...