if arcpy.Exists(r"D:\gis\Data\scratch.gdb"): arcpy.Delete_management(r"D:\gis\Data\scratch.gdb")
import shutil, os # This will remove the entire folder, plus any subfolders if os.path.isdir(r"D:\gis\Data\scratch.gdb"): shutil.rmtree(r"D:\gis\Data\scratch.gdb")
arcpy.Delete_management(arcpy.env.scratchGDB) arcpy.Delete_management(arcpy.env.scratchFolder) arcpy.Delete_management(arcpy.env.scratchWorkspace)
Perhaps try:arcpy.Delete_management(arcpy.env.scratchGDB) arcpy.Delete_management(arcpy.env.scratchFolder) arcpy.Delete_management(arcpy.env.scratchWorkspace)
This should delete the folders as defined in the user's environmental variables. arcpy.env.scratchGDB and scratchFolder exist within the scratchWorkspace (or the parent folder if the scratchWorkspace is a gdb)
Also these methods return a variable which should be 'true' (string) if the operation was successful for additional checking if necessary.
import arcpy import os inFC = arcpy.GetParameterAsText(0) tempFC = arcpy.env.scratchGDB + os.path.sep + "tempFC" arcpy.CopyFeatures_management(inFC, tempFC) # Do some work here... # Clean up when done... # arcpy.Delete_management(tempFC)
Isn't the scratch.gdb designed to *always* be available and *guaranteed* to exist? If so, why attempt to delete it (if that's even possible)?
Edit:
Also, the suggested "clean up" is not saying to delete the .gdb or the workspace. Rather it is saying, delete the contents of the .gbd:
http://resources.arcgis.com/en/help/main/10.1/index.html#//00570000006w000000import arcpy import os inFC = arcpy.GetParameterAsText(0) tempFC = arcpy.env.scratchGDB + os.path.sep + "tempFC" arcpy.CopyFeatures_management(inFC, tempFC) # Do some work here... # Clean up when done... # arcpy.Delete_management(tempFC)
Why not use the "in_memory" workspace instead?
I get what the OP is getting at. I had the same issue. Except that I was creating script/tool that left a file geodatabase as its result. I need to have the tool test to see if this resulting file geodatabase already exists before it creates it. If it exists, it will delete it and create an empty one before proceeding into the guts of the script.
Thus, the expected code was:
gdb_dir = r"C:\Temp" gdb_name = "result_gdb.gdb" gdb_path = r"C:\Temp\result_gdb.gdb" if arcpy.Exists(gdb_path): arcpy.Delete_management(gdb_path) arcpy.CreateFileGDB_management(gdb_dir,gdb_name) arcpy.CreateFeatureclass_management(gdb_path,"myPolys","POLYGON")
Except that, like the OP, when I step through this in the debugger, right after the "Delete_management" call, I notice that a file directory with the same name as the file geodatabase still exists. Thus, if this non-gdb file directory exists, the Exists() function call returns a <True> value and the code would end up calling the Delete_management function.
I discovered that the following code produces curious (surprising, unexpected) results:
gdb_dir = r"C:\Temp" gdb_name = "result_gdb.gdb" gdb_path = r"C:\Temp\result_gdb.gdb" arcpy.CreateFileGDB_management(gdb_dir,gdb_name) arcpy.CreateFeatureclass_management(gdb_path,"myPolys","POLYGON") arcpy.Describe(gdb_path).workspaceType # returns u'LocalDatabase' arcpy.Delete_management(gdb_path) # the first call to Delete arcpy.Exists(gdb_path) # returns True arcpy.Describe(gdb_path).workspaceType # returns u'FileSystem' # the type has changed arcpy.Delete_management(gdb_path) # the second call to Delete arcpy.Exists(gdb_path) # returns False # now it returns false
So, now the python code might have to check just how much the target file geodatabase has been deleted.
I don't want to enable overwriting for the script. I'm not using a 'scratch' workspace. It's not an 'in_memory' usage. I just expect that when I call a function to Delete something that that thing is actually deleted, not just turned into a different type of object.
So, now the code is:
while arcpy.Exists(gdb_path): arcpy.Delete_management(gdb_path) # just keep calling Delete until it finally goes away
I tried this while loop.... And it's still while-ing, several minutes later. Does this approach actually work, or is this an infinite loop?