Delete features based on Date using Python

3038
4
11-06-2014 12:45 AM
nageshnangi1
New Contributor II

Hi,

We have some feature classes stored in a feature dataset in ArcSDE . I want to delete the features which are older than 10 years from the feature classes using a python script.

Every feature class has an attribute field "LAST EDITED ON". The sample value is in format "10/14/2014 1:31:52 AM".

 

Note: The features which are older than 10 years should be deleted from every feature class on daily basis (If there are any).

We are using ArcGIS version 10.1

 

I have created following expression which selects the features for deletion.

 

expression = long(float((arcpy.CalculateValue_management(time.mktime((datetime.datetime.now()+relativedelta(years=-10)).timetuple())))[0])) - long(float((arcpy.CalculateValue_management(time.mktime(time.strptime(!LAST_EDITED_ON!, "%x %X"))))[0])) > 0

 

The expression runs fine on Python window but gives an error while running as a tool.

 

SyntaxError: invalid syntax (DeleteFeatureScript_Latest.py, line 18)

Failed to execute (Script).

 

Line 18 is the expression seen above.

 

Please suggest how to solve this problem.

 

 

Any help will be appreciated.

0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor

I suppose you are using the "Delete Features (Data Management)" tool to delete the features and the expression you showed is the one specified in that tool.

When you use python the syntax can be different for the expression than the one you use in the dialog manually. In this case you specify "!LAST_EDITED_ON!" as the field. This produces an invalid syntax in Python.

If you look at the Help, and scroll down to the second code sample, you see that the field is specified differently. They also use the arcpy.AddFieldDelimiters(FeatureClassOrTable, FieldName), to make sure that the field is addressed correctly.

Since you didn't specify the rest of the script it is impossible to verify if the error is indeed caused by this. You could try changing the code to:

fc = r"Path to your featureclass or table"

fld = "LAST_EDITED_ON"

fld_ok = arcpy.AddFieldDelimiters(fc, fld)

expression = long(float((arcpy.CalculateValue_management(time.mktime((datetime.datetime.now()

             + relativedelta(years=-10)).timetuple())))[0])) - long(float((

             arcpy.CalculateValue_management(time.mktime(time.strptime(fld_ok, "%x %X"))))[0])) > 0

Not sure if this will work though...

Kind regards, Xander

by Anonymous User
Not applicable

Hi Xander,

My goal is to delete features in based on a date range in a FGDB feature class. I ran into problem when trying to specify the query for the dates. Here is what I have so far:

import arcpy
fc = r 'Feature Class'

with arcpy.da.UpdateCursor(fc, "LASTUPDATE") as cursor:
      for row in cursor:

           if row[0] ==  Between 1/01/2019 0:00:00 PM AND 1/01/2020 0:00:00 PM:
               cursor.deleteRow()

What would be the correct format for deleting all records within the range? Any help is greatly appreciated. Thanks

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Collin Horace , 

You probably have to change the condition like this:

import arcpy
from datetime import datetime

fc = r'Feature Class'

with arcpy.da.UpdateCursor(fc, ("LASTUPDATE")) as cursor:
    for row in cursor:
        if  datetime(year=2019, month=1, day=1) < row[0] < datetime(year=2020, month=1, day=1):
            cursor.deleteRow()

You can also create a selection using the where clause and use the arcpy.DeleteFeatures_management() tool.

Test on a copy of you data.

by Anonymous User
Not applicable

Thanks Xander. That worked perfectly!