16

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 ?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 28, 2015 at 5:31
4
  • 2
    Select by attribute "FID" > 4 and use arcpy.Deletefeatures Commented Jun 28, 2015 at 5:46
  • Just out of curoisty, why couldn't you use cursors? Commented Jun 28, 2015 at 6:29
  • @fatih_dur because I'm beginner in Arcpy Commented 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() Commented Mar 27, 2018 at 13:01

1 Answer 1

35

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:
 cursor.deleteRow()

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"))
answered Jun 28, 2015 at 5:55
4
  • +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? Commented 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. Commented 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. Commented Jun 28, 2015 at 12:38
  • 1
    @wetland Yes, you can use the OID@ token--simply replace "OBJECTID" with "OID@". Commented Jun 28, 2015 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.