[GIS] arcpy.Delete_management not deleting a geodatabase folder

arcgis-desktoparcpypython

This script runs fine the first time but fails when run the second time. The issue seems to be that the KMLToLayer_conversion statement creates a file geodatabase (no surprise) that subsequently cannot be deleted even when the feature class is removed from the map, the layer file is deleted, and the geodatabase contents are deleted. I'd like clean up after myself when this script is done with all traces gone other then the new feature classes within MasterGDB. The problem is that this script can only be run once unless you exit ArcMap, manually delete the folder in Windows, then re-start ArcMap. Running the individual commands within the Python window inevitably shows a "" but the directory for the geodatabase remains. So what's this newbie missing here?
(In this test/debug script, there is only a single KML file "C:\Temp\KKKLLL.KML" — which converts just fine).

import arcpy, os
# Name: BatchKML_to_GDB.py
# Source: AS16818.ZIP from acripts.esri.com


import arcpy, os

# Set local variables and location for the consolidated file geodatabase
KMLDir = "C:\TEMP\KML3"
outLocation = "C:\\Temp\\MuleDeer"
MasterGDB = 'AllKLM5.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase as needed
if not (arcpy.Exists(MasterGDBLocation)):
    print MasterGDBLocation + " doesn't exist; creating it now"
    arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace    
# Set workspace (where all the KMLs are)
arcpy.env.workspace=KMLDir
for kmz in arcpy.ListFiles('*.KM*'):

  print "CONVERTING: " + os.path.join(arcpy.env.workspace,kmz)
  kmz2 = os.path.join(arcpy.env.workspace,kmz)
  arcpy.KMLToLayer_conversion(kmz2, outLocation)
  print "Done"

# Change the workspace to fGDB location
arcpy.env.workspace = outLocation

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Drop Master GDB from the array/list
wks.remove(MasterGDBLocation)

for fgdb in wks:  
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  featureClasses = arcpy.ListFeatureClasses('*', '', 'Palacemarks')
  for fc in featureClasses:
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])

arcpy.Delete_management(fcCopy)
arcpy.Delete_management("C:\\Temp\\Muledeer\\KKKLLL.lyr")
arcpy.Delete_management(fgdb)

Best Answer

Your identation level needs to be indented for:

arcpy.Delete_management(fgdb)

fgdb is a item within your loop that its reference cannot be accessed how you have it now.

Try:

for fgdb in wks:  
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  featureClasses = arcpy.ListFeatureClasses('*', '', 'Palacemarks')
  for fc in featureClasses:
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])

  arcpy.Delete_management(fgdb)

arcpy.Delete_management(fcCopy)
arcpy.Delete_management("C:\\Temp\\Muledeer\\KKKLLL.lyr")
Related Question