1

I have a script that runs through a database and deletes all the empty feature classes. Often I deal with databases that have hundreds of empty feature classes and it can take quite a while to run the tool that deletes the empty FCs. I have an idea to delete the entire feature data set that houses the empty FCs instead (thinking it would be faster). Any suggestions on how to do this?

Essentially, if a feature data set has a FC in it that has data it would still have to individually delete the empty FCs, but if the entire feature data set only houses empty FCs, I would like to just delete the data set itself. It may actually not be faster (just an idea). Here is my script as it stands.

import arcpy
arcpy.env.workspace = arcpy.GetParameterAsText(0)

datasetList = arcpy.ListDatasets('*', 'Feature')
for dataset in datasetList:
    fcList = arcpy.ListFeatureClasses('', '', dataset) 
    for fc in fcList:
        results = arcpy.GetCount_management(fc)
        count = int(results.getOutput(0))
        if count == 0:
            arcpy.Delete_management(fc)
            arcpy.AddMessage('Deleting ' + fc)
        else:
            pass
  • Welcome to GIS SE! What type of geodatabase? Personal, File, Enterprise? Are the FCs always stored in a Feature Dataset? – Midavalo Apr 13 '17 at 15:13
  • It sounds like you always have databases with one or more feature data sets, and inside those feature data sets you have feature classes that may or may not contain data. You still have to check each feature class for data before you delete either the feature class or the feature data set it is in. The GetCount tool is basically running a cursor on each feature class and those cursors are what is causing the time delay. Have you considered setting your script as a scheduled task to run before you get to work? – GBG Apr 13 '17 at 16:07
  • The databases are always file geodatabases. Due to our workflow, I can't really set this as a scheduled task plus I actually have this script rolled up into many other scripts and models that I run for other purposes. – pittpens080 Apr 13 '17 at 16:26
  • Could you prevent the empty feature classes from being created in the first place? Aside from that (or for the initial run), you could code a cursor that only reads the first row for confirmation and then breaks out of the loop, which may be quicker than GetCount, but in all honesty, I don't think that GetCount works that way. It runs crazy fast for me, even with millions of polygon features. – Tom Apr 13 '17 at 16:29
  • The slow down is probably more from the actual Delete function rather than GetCount. File Geodatabases appear to have some intricate back-end design that makes the efficiency of certain operations contingent on the size and structure of the entire database rather than on the size/structure of a given feature class. So, I would come back to: 1) preventing the creation of empty feature classes; 2) redesigning the data...do all those feature classes and tables belong in the same database/dataset? – Tom Apr 13 '17 at 16:36
4

I had a similar problem, a file geodatabase with hundreds of tables and the process of deleting them was painfully slow. The way I got around it was to create a new geodatabase, copy any feature classes I wanted to keep into the new geodatabase (typically just a few) and then delete the old geodatabase, this approach was much faster than trying to cycle through all the feature classes trying to delete them individually.

  • Honestly, I am laughing at myself a little bit. I already do something very similar in a different process I run and you are correct, it is way faster. I think this is absolutely the best solution! Thanks! – pittpens080 Apr 14 '17 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.