Delete a Feature Layer from a Feature Service in ArcGIS Online using the ArcGIS API for Python

Table of Contents

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 components of ArcGIS Online such as Feature Layers within a Feature Service. In this blog post we will focus on using the ArcGIS API for Python to delete a Feature Layer from within a Feature Service.

NOTE: Always be careful when performing any delete functions, especially when deleting a Feature Layer. A Feature Layer does not get sent to the Recycle Bin upon deletion, it is simply deleted! It is extremely important that you are confident you are deleting the correct Feature Layer. A good data back-up system helps if you have deleted the incorrect data.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

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 need to import the FeatureLayer class to create a FeatureLayer object which will represent the Feature Layer we wish to delete. The FeatureLayerCollection class represents a Feature Service in ArcGIS Online and will enable us to remove the Feature Layer of choice.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## creates a FeatureLayer object to create a FeatureSet from
from arcgis.features import FeatureLayer

## represents a Feature Service
from arcgis.features import FeatureLayerCollection
				
			

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

We have two required inputs, the Item ID for the Feature Service that we wish to delete the layer from, and the index of the layer that we want to delete. 

How do you get the index? Open your Feature Service landing page, if you only have one layer then the index is 0 (zero). If there are multiple layers, o (zero) is the topmost layer and increments by 1 (one) as we move down the list. 

NOTE: if you delete a Feature Layer, the indexes shift, for example you have 4 Feature Layers – 0,1,2,3 – and you delete 2, the indexes are now 0,1,2

WARNING: be very very very very careful when deleting a Feature Layer, once it is deleted it is gone!! Make sure you are confident that you are deleting the correct layer. FDM cannot be held accountable for you deleting the wrong data!

				
					## the item id for the feature service that contains the layer to query
fs_item_id = "ITEM_ID"

## the index of the layer to query (0 if only one layer or the first layer)
lyr_idx = 0
				
			

Get the Feature Layer definition

We first need to get the Feature Service ArcGIS Online item as an Item object. From this Item object we create a FeatureLayer object targeting the layer that we wish to delete (via the layer index).

In the code snippet below there is an alternate method for getting the FeatureLayer using list comprehension and the name of the layer.

				
					## get the feature service item that contains the layer to delete
fs_item = agol.content.get(fs_item_id)

## get the layer of interest to delete
fl = FeatureLayer.fromitem(
    item = fs_item,
    layer_id = lyr_idx
)

## use list comprehension and the layer name to be precise
##fl = [lyr for lyr in fs_item.layers if lyr.properties.name == "FEATURE_LAYER_NAME"][0]
				
			

Delete Feature Layer from the Feature Service

The main event, deleting the Feature Layer from the Feature Service. This is achieved by getting the Feature Layer definition as a dictionary and using the delete_from_definition() method available with a FeatureLayerCollection object.

				
					fl_properties = dict(fl.properties)

## create FLC object
flc = FeatureLayerCollection.fromitem(fs_item)

## update the JSON definition fof the feature service to delete the layer
flc.manager.delete_from_definition({"layers": [fl_properties]})
				
			

Geospatial Professionals, GIS Analysts, and enthusiasts will discover the power of automation and script-based operations to efficiently interact and update WebMaps in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of WebMap tasks with ease.

We will dissect WebMaps for all that they are and by the end of this course you will be comfortable with manipulating the JSON, which is the behind the scenes configuration of a WebMap, to produce scripts for workflows where there is currently no API Python method, such as grouping layers.

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.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## creates a FeatureLayer object to create a FeatureSet from
from arcgis.features import FeatureLayer

## represents a Feature Service
from arcgis.features import FeatureLayerCollection

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.fromitem
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayercollection
##  https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#featurelayercollectionmanager
##  https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#arcgis.features.managers.FeatureLayerCollectionManager.delete_from_definition
##
## API Version: 2.1.0.2, 2.2.0.1, 2.3.0
##
################################################################################

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

agol = GIS("home")

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

## the item id for the feature service that contains the layer to delete
fs_item_id = "ITEM_ID"

## the index of the layer to delete (0 if only one layer or the first layer)
lyr_idx = 0

################################################################################
## GET FEATURE LAYER DEFINITION ################################################

## get the feature service item that contains the layer to delete
fs_item = agol.content.get(fs_item_id)

## get the layer of interest to delete
fl = FeatureLayer.fromitem(
    item = fs_item,
    layer_id = lyr_idx
)

## use list comprehension and the layer name to be precise
##fl = [lyr for lyr in fs_item.layers if lyr.properties.name == "FEATURE_LAYER_NAME"][0]

################################################################################
## DELETE FEATURE LAYER FROM FEATURE SERVICE ###################################

fl_properties = dict(fl.properties)

## create FLC object
flc = FeatureLayerCollection.fromitem(fs_item)

## update the JSON definition fof the feature service to delete the layer
flc.manager.delete_from_definition({"layers": [fl_properties]})


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

Leave a Comment

Your email address will not be published. Required fields are marked *