Deleting rows with UpdateCursor

In addition to being used to edit rows in a table or feature class, UpdateCursor can also be used to delete rows. Keep in mind that when rows are deleted outside an edit session, the changes are permanent.

Getting ready

In addition to updating records, UpdateCursor can also delete records from a table or feature class. The UpdateCursor object is created in the same way in either case, but instead of calling updateRow(), you call deleteRow() to delete a record. You can also apply a where clause to UpdateCursor, to limit the records returned. In this recipe, we'll use an UpdateCursor object that has been filtered using a where clause to delete records from our FireIncidents feature class.

How to do it…

Follow these steps to create an UpdateCursor object that will be used to delete rows from a feature class:

  1. Open IDLE and create a new script.
  2. Save the script to C:ArcpyBookCh8DeleteWildfires.py.
  3. Import the arcpy and os modules:
    import arcpy
    import os
  4. Set the workspace:
    arcpy.env.workspace = "C:/ArcpyBook/Ch8/WildfireData/WildlandFires.mdb"
  5. Start a try block:
    try:
  6. Create a new instance of UpdateCursor inside a with block. Make sure you indent inside the try statement:
    with arcpy.da.UpdateCursor("FireIncidents",("CONFID_RATING"), '[CONFID_RATING] = 'POOR'') as cursor:
  7. Create a counter variable that will be used to print the progress of the script. Make sure you indent this line of code and all the lines of code that follow inside the with block:
    cntr = 1
  8. Delete the returned rows by calling the deleteRow() method. This is done by looping through the returned cursor and deleting the rows one at a time:
    for row in cursor:
      cursor.deleteRow()
      print("Record number " + str(cntr) + " deleted")
      cntr = cntr + 1
  9. Add the except block to print any errors that may occur:
    except Exception as e:
      print(e.message)
  10. The entire script should appear as follows:
    import arcpy
    import os
    
    arcpy.env.workspace = "C:/ArcpyBook/Ch8/WildfireData/WildlandFires.mdb"
    try:
      with arcpy.da.UpdateCursor("FireIncidents",("CONFID_RATING"), '[CONFID_RATING] = 'POOR'') as cursor:
        cntr = 1
        for row in cursor:
          cursor.deleteRow()
          print("Record number " + str(cntr) + " deleted")
          cntr = cntr + 1
    except Exception as e:
      print(e.message)
  11. You can check your work by examining the C:ArcpyBookcodeCh8DeleteWildfires.py solution file.
  12. Save and run the script. You should see messages being written to the output window as the script runs. 37 records should be deleted from the FireIncidents feature class:
    Record number 1 deleted
    Record number 2 deleted
    Record number 3 deleted
    Record number 4 deleted
    Record number 5 deleted
    

How it works…

Rows from feature classes and tables can be deleted using the deleteRow() method in UpdateCursor. In this recipe, we used a where clause in the constructor of UpdateCursor to limit the records returned to only features that included CONFID_RATING of POOR. We then looped through the features returned in the cursor and called the deleteRow() method to delete the row from the feature class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset