Please help- Delete Feature Class sample script

3599
6
02-08-2011 08:05 AM
EricHarmon
New Contributor
Hi- I'm very new to ArcPy. I am trying to write a script that will list all the point feature classes in my gdb and then delete them. I have found some examples, but can't get it to run. Your help is much appreciated.

import arcpy

arcpy.env.workspace = r"E:\KingCounty.gdb"

import string

try:

    fcList = arcpy.ListFeatureClasses("","point", "")
   
    for fc in fcList:
        Lst = arcpy.Describe(fc)
       
        if string.lower(Lst.shapeType) == "point":        
            print str(fc) + " is a " + Lst.shapeType + " feature class"
       
        else:
            print str(fc) + "The shapetype is unknown"

except:
    print "Script failed"

# Set local variables
inFeatures = "points"
outFeatures = r"E:\output.gdb\new_points"
tempLayer = "pointsLayer"
expression = arcpy.AddFieldDelimiters(pointsLayer, "Point_ID") + " = 'busstop'" + " = 'bridges'"

try:
    # Execute CopyFeatures to make a new copy of the feature class
    arcpy.CopyFeatures_management(inFeatures, outFeatures)

    # Execute MakeFeatureLayer
    arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)

    # Execute SelectLayerByAttribute to determine which features to delete
    arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION",
                                            expression)

    # Execute GetCount and if some features have been selected, then
    #  execute DeleteFeatures to remove the selected features.
    if arcpy.GetCount_management(tempLayer) > 0:
        arcpy.DeleteFeatures_management(tempLayer)
        
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message
Tags (2)
0 Kudos
6 Replies
JamesGonsoski
New Contributor III
Well, for one thing, I think you should change the expression value from:

tempLayer = "pointsLayer"
expression = arcpy.AddFieldDelimiters(pointsLayer, "Point_ID") + " = 'busstop'" + " = 'bridges'"

to read:

tempLayer = "pointsLayer"
expression = arcpy.AddFieldDelimiters(tempLayer, "Point_ID") + " = 'busstop'" + " = 'bridges'"

Either that, or just put quotes around the pointsLayer rather than use the tempLayer variable, as in:
expression = arcpy.AddFieldDelimiters("pointsLayer", "Point_ID") + " = 'busstop'" + " = 'bridges'"
0 Kudos
MikeWarner
New Contributor
Hello,

I'm trying to do something very similar to what the original poster wanted to accomplish.  I'm want to have a script that goes through a user-defined workspace and deletes all feature classes EXCEPT polygon layers.

Anybody have any tips?

*m
0 Kudos
MathewCoyle
Frequent Contributor
You want to describe your features and access the shapeType property. This sample will give you a place to start.
import arcpy

arcpy.env.workspace = #your workspace

fcList = arcpy.ListFeatureClasses()
drop_features = list()
for feature in fcList:
    desc = arcpy.Describe(feature)
    if desc.shapeType == 'Polygon':
        drop_features.append(feature)
        
for feat in drop_features:
    arcpy.Delete_management(feat)


You can also do away with the drop_features list if you want and just put the deletion where you would add the feature to the drop feature list.
0 Kudos
DarrenWiens2
MVP Honored Contributor
You want to describe your features and access the shapeType property. This sample will give you a place to start.
import arcpy

arcpy.env.workspace = #your workspace

fcList = arcpy.ListFeatureClasses()
drop_features = list()
for feature in fcList:
    desc = arcpy.Describe(feature)
    if desc.shapeType == 'Polygon':
        drop_features.append(feature)
        
for feat in drop_features:
    arcpy.Delete_management(feat)


You can also do away with the drop_features list if you want and just put the deletion where you would add the feature to the drop feature list.


Just FYI, the above code will delete all polygon feature classes. If you want to delete everything but polygons, use instead:
 if desc.shapeType != 'Polygon':
0 Kudos
MathewCoyle
Frequent Contributor
Whoops, yeah, good catch Darren.
0 Kudos
MikeWarner
New Contributor
Thanks for the timely and informative answers Mathew and Darren!
0 Kudos