0

I am trying to delete any record in my table that has a null value in a certain field (the Grid_Code field) and the code I have does not return any errors and seems to run fine, but doesn't actually delete the records with null values.

Here is my code:

with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
    for row in cursor:
         if (row[0] == '<Null>'):
            cursor.DeleteRow(row)
         #I also tried adding this and got the same result:
         else:
             pass

Any ideas about what I am doing wrong?

3
  • 4
    arcpy.da.UpdateCursor accepts a WHERE clause -- just use where="GridCode is NULL", and you can eliminate the (incorrect) if test. If you really want to scan the whole table, test on row[0] == None. You could also select rows with the WHERE clause, then use DeleteRows_management
    – Vince
    Commented Nov 13, 2018 at 20:11
  • 2
    (make sure you have a backup before starting to debug deletion utilities)
    – Vince
    Commented Nov 13, 2018 at 20:31
  • You're currently checking to see if that field literally contains the string "<Null>", which of course it doesn't. Null fields return None, IIRC, so you could do if row[0] is None to check for that explicitly, or just if not row[0] to delete any rows where there's a blank-like value (like a string with no length or a number that's exactly zero).
    – mikewatt
    Commented Nov 13, 2018 at 21:06

2 Answers 2

1

Simpler Than Cursors Solution:

I'm not sure if this qualifies as an answer, since you explicitly mentioned using cursors. However, I reckon it is easier to delete features without using a cursor:

arcpy.MakeFeatureLayer_management(fc, "tmp_layer", "Grid_Code IS NULL")
arcpy.DeleteFeatures_management("tmp_layer")

To be complete, you should probably tidy up afterwards, with:

arcpy.Delete_management("tmp_layer")

Cursors Solution A:

However, if you really want to stick with cursors, you need to be testing for the NULL value, not the string "<NULL>". In Python, the NULL value is None. So you could replace the if line in your code with:

        if row[0] is None:

This would give you:

with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
    for row in cursor:
         if row[0] is None:
            cursor.deleteRow()

It's also worth noting that according to the official Python Style Guide, you should use is not == to test equality with the None object (NULL value).

Cursors Solution B:

Still using cursors, but a bit simpler with one less line, using SQL instead of Python to check for the NULL value:

with arcpy.da.UpdateCursor(fc, 'Grid_Code', 'Grid_Code is NULL') as cursor:
    for row in cursor:
         cursor.deleteRow()
4
  • I was thinking about your simplest solution, what happens in the case when there are no NULL values, does the DeleteFeatures tool blitz the entire layer?
    – Hornbydd
    Commented Jan 31, 2019 at 10:16
  • 1
    No, I don’t think so. That would be the case if you were trying to delete selected features and no features were selected. However, in this case, there would simply be no features in the layer to delete. That’s why this method is better than selecting features and then running a delete. Commented Jan 31, 2019 at 10:29
  • should be a lower case d and empty bracket on deleteRow()
    – Theo F
    Commented Oct 29, 2019 at 15:48
  • Can you use these approaches to delete rows where all fields are NULL?
    – joechoj
    Commented Jun 21, 2023 at 22:41
0

Use None.

with arcpy.da.UpdateCursor(fc, 'Grid_Code') as cursor:
    for row in cursor:
         if row[0] == None:
            cursor.DeleteRow(row)

Your Answer

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

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