arcpy.MakeFeatureLayer in-memory layer still exists when subsequent step fails during testing

arcpy.Delete_management(featureLayer)

Including

arcpy.env.overwriteOutput = True

within your script will overwrite any previous version of a file without having to delete "bad" versions at all.


Deleting the feature layer in the except code block won't work. If you're in the except code block, the feature layer failed to be created.

I recommend adding a finally code block that will be executed whether the try block is sucessful or not. If you do this, you should make sure the feature layer exists before you can delete it.

try:
    arcpy.MakeFeatureLayer_management(inFeatureClass, "outFeatureLayer", {whereClause})
except:
    print arcpy.GetMessages()
finally:
    if arcpy.Exists("outFeatureLayer"):
        arcpy.Delete_management("outFeatureLayer")