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
Best Answer
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