Does anyone have a python API recipe for deleting features in an ArcGIS Online hosted feature layer? I'm specifically looking to clear out all the features in several layers.

8074
10
Jump to solution
08-03-2017 11:44 AM
AmberLauzon
New Contributor II

We have several emergency operation layers that we want to clear out quickly before an event. We will use juniper to share it with our staff too. Thank you.

0 Kudos
1 Solution

Accepted Solutions
AtmaMani
Esri Contributor

Although this is not a full recipe, here is the snippet:

You can delete features by calling delete_features() method on your FeatureLayer object like shown below:

feature_layer_item = gis.content.search("your search criteria")[0]
flayers = feature_layer_item.layers

flayer = flayers[0]
flayer.delete_features(where="objectid > 0")

You can specify any other query to selectively delete some features. Alternately, if you want to delete call, then call the truncate() method on the FeatureLayerManager

flayer.manager.truncate()

Truncate will delete all features and reset the objectid or fid count back to 0. Help for truncate

View solution in original post

10 Replies
AtmaMani
Esri Contributor

Although this is not a full recipe, here is the snippet:

You can delete features by calling delete_features() method on your FeatureLayer object like shown below:

feature_layer_item = gis.content.search("your search criteria")[0]
flayers = feature_layer_item.layers

flayer = flayers[0]
flayer.delete_features(where="objectid > 0")

You can specify any other query to selectively delete some features. Alternately, if you want to delete call, then call the truncate() method on the FeatureLayerManager

flayer.manager.truncate()

Truncate will delete all features and reset the objectid or fid count back to 0. Help for truncate

AmberLauzon
New Contributor II

Thank you! Just what I needed!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Amber Lauzon ,

Could you mark Atma Mani 's post as the correct answer please? This will help other users to quickly find the correct solution.

Thanks!

AtmaMani
Esri Contributor

One caveat, Truncate is not yet supported on ArcGIS Enterprise, it works for ArcGIS Online though.

0 Kudos
AmberLauzon
New Contributor II

Pardon my inexperience, but I am trying to install the py for api package and I cannot using ArcGIS Pro 1.4, do you have any suggestions? The "Updates" and "Add" tabs are blank too...

0 Kudos
PaulHaakma
Regular Contributor

One more caveat: looks like truncate doesn't work if sync is enabled on the dataset.

Not much of a problem though as the delete features seems to work fine and reasonably quickly.

flayer.delete_features(where="objectid > 0")
MuhammadNaufalIhsan
New Contributor II

Hi All.

Does anyone have any guide if I want to delete using multiple clauses? Like for example, I want to delete rows where column A is not 'abc' and column B is not 'def'.

Thank you.

0 Kudos
ChristopherSchreiber
Occasional Contributor II

Hi all!

I used the sample that Atma Mani‌ provided and created a small script to delete features from a Hosted Feature Service in AGOL that has multiple sub-layers. 

import os
import json
import arcgis
from arcgis.gis import GIS
import arcgis.features

layerName = 'title:Your Layer Name'
maxLayerAmount = 10 # number of sublayers

agolURL = "https://<your org>.maps.arcgis.com"
agolUN = "<Your UserName>"
agolPass = "<Your Password>"

# connect to your GIS
gis = GIS(agolURL, agolUN, agolPass)
 
layerCount = -1
while (layerCount < maxLayerAmount):
    layerCount += 1
    feature_layer_item = gis.content.search(layerName, item_type = 'Feature Service') [0]
    flayers = feature_layer_item.layers
    flayer = flayers[layerCount]
    print("Layer ID: " + str(layerCount) + " Layer Name: " + flayer.properties.name)
    flayer.delete_features(where="objectid > 0")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I also added some logging to display the name of the layers as it loops through them.

The logging looks something like this: 

Layer ID: 0 Layer Name: Point Layer
Layer ID: 1 Layer Name: Line Layer
Layer ID: 2 Layer Name: Polygon Layer

I am still new to Python, so if there are any suggestions/comments, please let me know!

Hope this helps!

Chris

0 Kudos
ImtiazSyed
New Contributor III
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 or more, in case the dataset is large

i = 0
step = 20000

#replace maxoid with count if attempting to delete features based on feature count
while i <= maxoid:
    i += step
    flayer.delete_features(where=f"OBJECTID <= {i}")
    print(i)

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

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

Use this script in combination of @Christopher Schreibescript in-case there are other dependencies in your feature layer like sync capability and relationships.

ArcGIS Feature Layer query out_statistics

Best,

Imtiaz