Select to view content in your preferred language

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

223
0
10-31-2025 08:35 AM
Clubdebambos
MVP Regular Contributor
3 0 223

 

The workflow:

  • Access ArcGIS Online
  • Create a FeatureLayer object representing the Feature Layer to delete. You can create the FeatureLayer object any way you wish.
    • the Item object layers property
    • a FeatureLayerCollection object layers property
    • instantiate FeatureLayer object using fromitem (used below) or a URL 
  • Cast the JSON properties of the FeatureLayer object to a Python dictionary.
  • Create a FeatureLayerCollection object representing the Feature Service that contains the Feature layer to delete from.
  • Use the FeatureLayerCollectionManager delete_from_definition() to remove the FeatureLayer from the Feature Service.

 

WARNING! Deleting a Feature Layer within a Feature Service is Permanent (unless you made a backup). The deletion does not get sent to a recycle bin so proceed with caution and always backup before deletion.

 

## 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
##
################################################################################

################################################################################
## 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 OBJECT ####################################################

## 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")

 

Contributors
About the Author
Glen Bambrick is GIS Consultant with extensive experience utilising skills for GIS project management, quality map production, data management, geospatial analysis, standardisation, and workflow automation across a range of industries such as agriculture, oil & gas, telecommunications and engineering. Going beyond map production there is a passion for the technologies behind GIS, the databases, the programming languages, the analysis and statistical output, and continued professional development associated with GIS. You will mainly find me contributing in the Python and ArcGIS API for Python Communities.