How to delete features of an ArcGIS Online Hosted Feature Layer?

544
0
08-27-2019 02:42 PM
ImtiazSyed
New Contributor III
1 0 544

Hi All,

I used this script to delete all the features from a hosted feature layer where it had relationships and had sync capability enabled on it. 

https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html?highlight=query#arcgis...

import arcgis
from arcgis.gis import GIS

gis = GIS("https://site.maps.arcgis.com", "username", "password")

feature_layer_item = gis.content.search("<item-id>")[0]
flayers = feature_layer_item.layers

flayer = flayers[0]

#count = flayer.query(return_count_only=true)

max_objid = flayer.query(out_statistics=[{"statisticType":"MAX","onStatisticField":"OBJECTID","outStatisticFieldName":"MAX_OBJ"}], return_geometry=False)
maxoid = max_objid.features[0].attributes['MAX_OBJ']

#Delete in steps of 20000, in case the dataset is large

i = 0
step = 20000

#replace maxoid with count if attempting to delete features based on feature count

#using for loop
#for i in range(1,20000,maxoid):

while i <= maxoid:
    i += step
    flayer.delete_features(where=f"OBJECTID <= {i}")
    print(i)

print("Feature Layer was truncated")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks,

Imtiaz

About the Author
GIS Developer