Truncate a Feature Layer in ArcGIS Online with the ArcGIS API for Python

Table of Contents

The Video

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating Feature Services and Feature Layers in ArcGIS Online. In this post we will truncate (delete all data) for a Feature Layer.

Automate ArcGIS Online Feature Service Workflows with the ArcGIS API for Python | A Complete Guide from Beginner to Advanced | Full Course

Free on YouTube! A set of videos detailing ArcGIS Online Feature Services workflows using the ArcGIS API for Python. We will do a deep dive into properties and methods available, along with custom workflows for automating with Feature Services and the ArcGIS API for Python.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. We will also utilise the FeatureLayer class that will enable us to access the Feature Layer we wish to truncate.
				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## the FeatureLayerCollection class
from arcgis.features import FeatureLayer
				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Required inputs

Only one  required input, the url to the Feature Layer to truncate.

				
					fl_url = "FEATURE_LAYER_URL"
				
			

Create a FeatureLayer Object

A FeatureLayer object is required. While below we use the FeatureLayer class to create the FeatureLayer object, you could access the FeatureLayer object via an Item object or a FeatureLayer Collection object.
				
					fl = FeatureLayer(
    url = fl_url,
    gis = agol
)

				
			

Truncate the Feature Layer

Use the FeatureLayer object manager property which is a FeatureLayerManager object to access the truncate() method.
				
					fl.manager.truncate()
				
			

Before truncate()

Befaore ArcGIS API for Python truncate()

After truncate()

After ArcGIS API for Python truncate()

Automate ArcGIS Online Feature Service Workflows with the ArcGIS API for Python | A Complete Guide from Beginner to Advanced | Full Course

Free on YouTube! A set of videos detailing ArcGIS Online Feature Services workflows using the ArcGIS API for Python. We will do a deep dive into properties and methods available, along with custom workflows for automating with Feature Services and the ArcGIS API for Python.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					from arcgis.gis import GIS
from arcgis.features import FeatureLayer

########################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.manager
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.managers.html#arcgis.features.managers.FeatureLayerManager
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.managers.html#arcgis.features.managers.FeatureLayerManager.truncate
##
########################################################################################

########################################################################################
## ACCESS ARCGIS ONLINE ################################################################

agol = GIS("home")

########################################################################################
## USER INPUTS #########################################################################

fl_url = "FEATURE_LAYER_URL"

########################################################################################
## CREATE FEATURE LAYER OBJECT #########################################################

fl = FeatureLayer(
    url = fl_url,
    gis = agol
)

########################################################################################
## TRUNCATE FEATURELAYER ###############################################################

status = fl.manager.truncate()

print(status)

########################################################################################
print("\nSCRIPT COMPLETE")
				
			

Leave a Comment