0

I have a feature class that resides in an Enterprise geodatabase, I need to delete a row that has specific text in a specific field at a specific. Data is being submitted daily to this feature class via Survey123, a "dummy" point is created daily for other purposes. I would like to delete that "dummy" point daily at a specific time using ArcPy. I am novice with ArcPy.

import arcpy

myFC = ("target dataset")

commentfield = 'Comments'
sql = """{0} LIKE '%NameOfTextToSearchFor%'""".format(arcpy.AddFieldDelimiters(myFC,commentfield))

with arcpy.da.UpdateCursor(myFC, commentfield, sql) as cursor:
    for row in cursor:
        cursor.deleteRow(row)

I couldn't figure out how to indent the last two lines

5
  • 1
    The easiest way to delete a row in an unversioned feature class is to use SQL. Versioned FCs can also delete with SQL, but you need to use a versioned view. I believe the UpdateCursor documentation is has a deletion example. What have you tried? – Vince Mar 11 at 17:26
  • I can't share my code b/c it's too many characters, is there a workaround to that? – Mapsy Daisy Mar 11 at 17:42
  • 2
    Policy is to share a code snippet that demonstrates the issue. It would not take more than ten lines, as an Edit to the Question, to show the necessary Python code. – Vince Mar 11 at 18:05
  • Could you please describe in more detail the search term you are using to identify the row that you want to delete? Is the text "TEST" buried in other text or is it stand alone at every occurance? – Aaron Mar 11 at 22:28
  • Writing code snippets to get quicker answers may be worth you reviewing but I think you've already made quite a good attempt at doing that. – PolyGeo Mar 12 at 7:37
1

In this example, I check for any occurrence of the text 'Test' in the field "some_field" using an if statement. For example, this will delete rows in any of the following cases: '1234test_abcd', 'Test', 'test', 'one_moreTest'. Using the `.lower() method allows you to include any type of case in the search term (e.g. 'Test', 'test', 'TeSt', etc).

You would use the .deleteRow() method to delete any instances of the search term.

For example:

import arcpy

fc = r'C:\path\to\your\geodatabase.gdb\featureclass'

with arcpy.da.UpdateCursor(fc, "some_field") as cursor:
    for row in cursor:
        if row[0] is not None and 'test' in row[0].lower():
            cursor.deleteRow()

To be more specific in your search term, replace the following:

if row[0] is not None and 'test' in row[0].lower():

with:

if row[0] == 'Test':

Finally, if you need to specify a time constraint, try the following (untested) code:

import arcpy

fc = r'C:\path\to\your\geodatabase.gdb\featureclass'

time_value = 'add-your-time-stamp-here'

with arcpy.da.UpdateCursor(fc, ["time_field", "some_field") as cursor:
    for row in cursor:
        if row[0] == time_value: # Only iterates through rows with your time stamp
            if row[1] == 'Test': # Checks for your search term
                cursor.deleteRow() # Delete row
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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