Table.delete_features() RuntimeError: Unable to Delete Features

399
3
Jump to solution
01-06-2020 10:05 PM
JamesKwong1
New Contributor II

Thank you Joshua, I was thinking about edit_features, I'll give it a go.

Taking advantage of opportunity (please let me know if you'd prefer me to open new question) - I try to run something like this:

# below returns response suggesting query is correct
table.validate_sql(sql="my_timestamp_column < TIMESTAMP '2019-01-06 11:00:00'")

# below throws exception
table.delete_features(where="my_timestamp_column < TIMESTAMP '2019-01-06 11:00:00'")

Line 5 fails with exception:

Traceback (most recent call last):
File "harvester/test/simulate_entry_point.py", line 40, in <module>
route_event(
File "/lib/python3.8/site-packages/arcgis/features/layer.py", line 1446, in delete_features
return self._con.post(path=delete_url, postdata=params, token=self._token)
File "/lib/python3.8/site-packages/arcgis/_impl/connection.py", line 1183, in post
self._handle_json_error(resp_json['error'], errorcode)
File "/lib/python3.8/site-packages/arcgis/_impl/connection.py", line 1204, in _handle_json_error
raise RuntimeError(errormessage)
RuntimeError: 
Unable to delete features.
(Error Code: 400)

There is only 40 items meeting criteria so should not be hitting any limits.

I think this might be due to open-ended nature of the query (although I'd like arcgis to delete all features meeting criteria, I have seen posts here where people had to run multiple queries with bundles of ~2000 items).

0 Kudos
1 Solution

Accepted Solutions
JamesKwong1
New Contributor II

The solution is to provide actual date in UTC

import datetime
from arcgis.gis import GIS
from arcgis.features import Table

g = GIS(username="my_user_name", password="my_secret")
pit = g.content.get('my_published_csv_item_id')
t = Table.fromitem(item=pit)

t.delete_features(where="my_datet_time_column < '{}'".format((datetime.datetime.utcnow() - datetime.timedelta(seconds=3600)).strftime("%Y-%m-%d %H:%M:%S")))
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

If the SQL validates and there are only 40 or so items, I don't think it is related to the query.  As a test, see if you can pass an OID using the deletes parameter.  If it throws the same error, then it is likely related to your permissions or service configuration.

JamesKwong1
New Contributor II

Many thanks Joshua, I can delete by oids (and this is my workaround at the moment - I run table.query with time condition and then collect oids and delete with another call)

However since calls to arcgis are very expensive in terms of time I'd love to save one call if possible and run table.delete_features with query...

0 Kudos
JamesKwong1
New Contributor II

The solution is to provide actual date in UTC

import datetime
from arcgis.gis import GIS
from arcgis.features import Table

g = GIS(username="my_user_name", password="my_secret")
pit = g.content.get('my_published_csv_item_id')
t = Table.fromitem(item=pit)

t.delete_features(where="my_datet_time_column < '{}'".format((datetime.datetime.utcnow() - datetime.timedelta(seconds=3600)).strftime("%Y-%m-%d %H:%M:%S")))
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍