AnsweredAssumed Answered

Deleting Features: Does cursor mess with order?

Question asked by matthiasBuehler on Sep 25, 2016
Latest reply on Sep 27, 2016 by LukeW

Hi,

 

Question:

I have read that cursors can only be iterated forward, not in reversed order.

 

Not if I have e.g. 5 features and the following code structure:

delCursor = arcpy.da.UpdateCursor(fc, field)
for row in delCursor:

   if testIfRowNeedsToBeDeleted(..):

      delCursor.deleteRow() # messes with the number of elements in the loop!!

 

How can I guarantee that the correct features are deleted when I delete features with 'early' OBJECTIDs out of the list, as after the first deletion, the number of elements in the field is already not the same as the cursor's number.

 

How is this handled internally?

 

* * *

 

In the end, I just need to delete specific rows out of a feature class, depending on specific field values and I want to avoid any issues with the order.

 

My favourite would be this structure in pseudo code:

to deleteFeatureOBJECTIDs = []

loop over cursor:

   if feature needs to be deleted based on multiple field values:

      deleteFeatureIDs.append(current feature)

 

arcpy.deleteFeaturesInFC(deleteFeatureOBJECTIDs)

 

[[ I have also tried code-based creation of the SQL 'where_clause', with a string list, but this type of search crashes ArcGIS PRO quite easily. ]]

 

Any input welcome!

Thanks!

matt

Outcomes