arcpy.DeleteFeatures_management isn't deleting Feature Layers

5885
2
08-31-2011 12:43 PM
MikeMacRae
Occasional Contributor III
Hey everyone,

I am trying to test to delete a feature layer in a python script, but it doesn't seem to delete it. I created a feature layer using MakeFeatureLayer_management for a couple feature classes and did a test to delete them immediately afterwards because my original script kept errorring out saying that the feature already existed.

In my following script, I created 2 "for" loops and then created a feature layer and deleted afterwards. I tested this by setting a print statement to print out the feature layer immediately after I deleted it. My expectation was to have nothing returned on the print statement after I deleted it, but it actually returns the feature layer which is why my original script gets hung up. Can anyone suggest why the feature layer isn't getting deleted?


My script is as follows:

import arcpy

from arcpy import env

arcpy.workspace = r"Z:\ESRI\Figure_Sourcing\Figures\Geodatabase\D01_PDA_CR.gdb"

fcList3 = arcpy.ListFeatureClasses("*", "Polygon")
fcList4 = arcpy.ListFeatureClasses("*", "Point")

for fc3 in fcList3:
        for fc4 in fcList4:
                if fc3 == "Parcels" and fc4 == "Points":
                        print fc3
                        print fc4
                        test1 = arcpy.MakeFeatureLayer_management(fc3,"polygon2")
                        test2 = arcpy.MakeFeatureLayer_management(fc4,"point2")
                        arcpy.DeleteFeatures_management("polygon2")
                        arcpy.DeleteFeatures_management("point2")
                        print test1
                        print test2
Tags (2)
0 Kudos
2 Replies
LoganPugh
Occasional Contributor III
Just use env.overwriteOutput = True and it will let you make feature layers even if one with the same name already exists.

And DeleteFeatures_management actually deletes the features from your underlying feature class -- probably not what you meant to do.

If you really want to delete a feature layer use Delete_management instead.

Also, your test is flawed: the value returned by MakeFeatureLayer_management is just a string (the name of the feature layer), so printing it will always print its name regardless of whether you deleted it or not.
0 Kudos
MikeMacRae
Occasional Contributor III
Thanks Logan!
This actually solved a whole host of issues with my script. Works like a charm now.
0 Kudos