Monday, September 24, 2012

Delete Rows Using the UpdateCursor - arcpy.da

The arcpy.da.UpdateCursor() provides the method to alter and update field values.  This quick post will show you how to delete a row.

All you need to do is specify the fields, here I take the default of * and the feature class.  You can reduce the number of rows by passing a query as well.

Code Sample Run From Python Window:

with da.UpdateCursor(r'C:\temp\scratch.gdb\floodsub', "*") as urows:
...     for row in urows:
...         urows.deleteRow()
...         break


This little sample just erases the first row in the feature class.  The 'with' statement automatically closes the cursor object, so it does not have to be manually deleted.  This ensures the locks are removed on success or on failure.

Enjoy