I want to delete all rows in a feature class but remain rows 1 to 5. In other hand I want to remain first five rows and delete others. I know that I have to use search cursor and update cursor but I couldn't use them. How can I delete the rows using ArcPy ?

  • 2
    Select by attribute "FID" > 4 and use arcpy.Deletefeatures
    – FelixIP
    Jun 28, 2015 at 5:46
  • Just out of curoisty, why couldn't you use cursors?
    – fatih_dur
    Jun 28, 2015 at 6:29
  • @fatih_dur because I'm beginner in Arcpy
    – BBG_GIS
    Jun 28, 2015 at 8:15
  • import arcpy fc = r'C:\temp\test.gdb\tmp' expression = "objectid >5" with arcpy.da.UpdateCursor(fc, "OBJECTID",where_clause = expression) as cursor: for row in cursor: if row[0] > 5: cursor.deleteRow()
    – kumar
    Mar 27, 2018 at 13:01

1 Answer 1


You can use an Update Cursor to delete rows based on your conditions. In this example, any rows where OBJECTID > 5 is deleted.

import arcpy

fc = r'C:\temp\test.gdb\tmp'

with arcpy.da.UpdateCursor(fc, "OBJECTID") as cursor:
    for row in cursor:
        if row[0] > 5:

Alternatively, use Select Layer By Attribute (Data Management).

import arcpy, os

fc = r'C:\temp\test.gdb\tmp'
outws = r'C:\temp\test.gdb'

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(fc, "fc_lyr")

# Use a SQL query to select OBJECTID 1 - 5
arcpy.SelectLayerByAttribute_management("fc_lyr", "", ' "OBJECTID" <= 5 ')

# Write the selected features to a new feature class
arcpy.CopyFeatures_management("fc_lyr", os.path.join(outws, "fc_out"))
  • +1 for keeping the original features as long as system resources allow. One question, what if the OIDs do not start from 1 and are not consecutive?
    – fatih_dur
    Jun 28, 2015 at 6:28
  • @Aaron Thanks. Is it possible to use object id field type instead of "OBJECTID" name. because in some layers the name of object id is different.
    – BBG_GIS
    Jun 28, 2015 at 7:37
  • @faith_dur Be careful with equating "OID" and "OBJECTID"; the source format dictates behavior. The OID rowid column of shapefiles is immutable (and zero based), while an enterprise or file geodatabase OBJECTID retains value after initialization (and is usually one-based). You do have a good point that a SQL subquery with an ORDER BY and LIMIT would be necessary to identify the current first 5 rows.
    – Vince
    Jun 28, 2015 at 12:38
  • 1
    @wetland Yes, you can use the OID@ token--simply replace "OBJECTID" with "OID@".
    – Aaron
    Jun 28, 2015 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.