The workflow:
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.