Purge Scratch GeoDatabase

In a lot of the scripts I write, especially data preparation pipelines, I use the scratch Geodatabase - a lot. At the end of these pipelines, especially when testing and re-running the processes multiple times, I like to clean out this workspace to ensure my results are the new results, and also just to keep from filling up my disk.

Doing this in Python can be difficult using the various list methods available in arcpy since none of them are able to simply list all the top level objects. You need a different list method for all the possible object types you may potentially encounter. The walk method in the data access module is able to list all the resources recursively all the way down through multiple levels of directories, geodatabases, and even into feature datasets, making it a little bit of overkill.

You can take advantage of the walk method to only list the top level objects by simply breaking at the end of the first iteration, and during this first iteration, delete all the top level objects to easily clean out the scratch Geodatabase, or any other Geodatabase for that matter.

Hopefully this makes you life easier!