Select to view content in your preferred language

Hosted Feature Layer - delete one of the layers within it?

13828
10
01-22-2016 09:14 AM
by Anonymous User
Not applicable

Hello all,

I have a hosted feature layer that has 16 layers within it. I want to delete the first layer but I would like to avoid having to delete the whole service and republishing from ArcMap because this layer is live and there are edits being made on it. I would have to download the data in fgb and then repath that data in my ArcMap mxd and then republish. Then I would probably have to recreate the web map and the web app because I have had problems in the past of my existing web maps and apps reading any changes to my feature layers.

What about this blog and PDF referenced in blog? https://blogs.esri.com/esri/arcgis/2014/10/19/updating-hosted-feature-services-in-arcgis-online/

PDF referenced in blog: https://blogs.esri.com/esri/arcgis/files/2014/10/How-to-Update-Hosted-Feature-Service-Schemas1.pdf

In the service URL of the layer I want to delete I see, "Delete from Definition". But does that just delete a few fields in that layer and not the layer itself?

delete from definition.JPG

Does anyone have any ideas?

Thank you.

0 Kudos
10 Replies
Katie_Clark
MVP Alum

This is a very old post, but as far as I'm concerned, if posts like this still show up in a Google search, then it's still relevant! Thank you @mikaël for getting me on the right track - it's important to do it with the REST API, modifying the JSON through ArcGIS Online Assistant did not fully delete the layer from the service (I found that it removed it from the Item Details page, but when viewing the service definition, the item was still there. I think AGOL Assistant only removes it from part of the JSON)

Anyway, I'm writing this post to share a script I wrote to put this functionality into a Python tool that can be run in ArcGIS Pro. I hope it maybe helps someone with this process, especially those who don't have a lot of experience with working with the API.

Regarding toolbox setup, add this script and set the Parameters like this, and it should work like a charm.

Katie_Clark_0-1746217080344.png

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
import arcpy

# Tool input parameters

item_id = arcpy.GetParameterAsText(0)  # String (required)
layer_id = arcpy.GetParameter(1)  # Long (required)


def main():
    gis = GIS("home")  # Uses ArcGIS Pro sign-in session

    item = gis.content.get(item_id)

    fs = FeatureLayerCollection.fromitem(item)

    layer_ids = [lyr.properties.id for lyr in fs.layers]
    arcpy.AddMessage(f"Available Layer IDs: {layer_ids}")

    if layer_id not in layer_ids:
        arcpy.AddWarning(f"Layer ID {layer_id} does not exist in the feature service.")

    delete_payload = {
        "layers": [
            {"id": layer_id}
        ]
    }

    try:
        result = fs.manager.delete_from_definition(delete_payload)
        if result['success']:
            arcpy.AddMessage(f"Successfully deleted layer ID {layer_id}.")
    except Exception as e:
        arcpy.AddError(f"Error: {e}")


if __name__ == "__main__":
    main()

 

Best,
Katie

If this answer helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!