2

How to delete features/records in ArcGIS Online hosted service using Python? I need to delete some features using Python from a feature services hosted in ArcGIS Online. I read a similar post that included syntax for ArcGIS Server:

> import urllib, urllib2, json
> 
> serviceEndPoint =
> "http://<server>/ArcGIS/rest/services/<name>/FeatureServer/<ID>/"
> 
> #Query the server for the objects to be deleted params = urllib.urlencode({'where': <whereclause>, 'f': 'json',
> 'returnIdsOnly': 'true'}) response = urllib2.urlopen(serviceEndPoint +
> "query?", params).read()
> 
> #Convert the response into JSON then extract the IDs. Convert to a string data = json.loads(response) IDs = data["objectIds"] range =
> ','.join([str(x) for x in IDs])
> 
> #Build the delete code, and submit it delParams = urllib.urlencode({'objectIds': range, 'f': 'json'})
> urllib2.urlopen(serviceEndPoint + "deleteFeatures?", delParams)

In my scenario, I am adding the token snippet as well as the username/password at the top. I tried modifying the script above in the section to say OBJECTID > 0 but after testing it, it still returns me an error:

params = urllib.urlencode({'where': OBJECTID = 5, 'f': 'json', 'returnIdsOnly': 'true'})
                                             ^  SyntaxError: invalid syntax
0

I believe the easiest way to do this is using the "arcgis" python library released by esri. This can be achieved using the class "arcgis.features.FeatureLayer" and the method to delete features would be

delete_features(deletes=None, where=None, geometry_filter=None, gdb_version=None, rollback_on_failure=True)

See documentation below: https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.html?highlight=delete%20features

2

Use the existing arcrest python library. Recommend using version 3.0.1 The sample for deleting rows is here: https://github.com/Esri/ArcREST/blob/master/samples/delete_rows_from_service.py

  • where can I find the following directory? Applications > AutomatedSetup – Ernesto CD Feb 8 '16 at 18:08
  • I installed ArcREST as specified via command prompt successfully using python setup.py install command from the directory. When running the tool Delete Data from Feature Service, either using the script tool or python IDLE, the tool fails. ImportError: No module named six – Ernesto CD Feb 8 '16 at 18:46
  • Ah yes, you downloaded the latest version. Recommend getting the previous version 3.0.1(there is a link on the reports home page) I believe... the 3.5 is still being worked on – Ben S Nadler Feb 10 '16 at 4:56
0

Since the coming of ArcGIS Pro it is also possible to delete rows directly from the attribute table. Just go to Portal within the Catalog Pane and add the hosted Feature Layer to your map, then open the attribute table and delete any rows you would like deleted.

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.