Grab sections of Service to process

304
1
Jump to solution
01-19-2024 12:30 PM
kapalczynski
Occasional Contributor III

I am trying to delete records via REST Service.  the smaller tables delete no issues but Feature Classes with images seem to NOT delete as they have more than 10,000 records

If I look at the ObjectIDs and grab the first half and then the second half they both delete.

But I want this to be automated... So I am trying to think this through... Trying to figure out how to query for the first say 2000 records and then do something... then grab 2001st record through 4000 and do something etc.

Anyone have any ideas?

Im POST like below using a where clause.. so what ever solution would have to have a viable WHERECLAUSE ... not sure how to do this without manually setting the where clause to something like this:

How can I grab my features in CHUCKS

if targetLayer == "GIS_DATA.SDEInventory":
    whereclause="{}".format("OBJECTID<15000")
    processDeletion(url, whereclause)
    whereclause="{}".format("OBJECTID>=15000")
    processDeletion(url, whereclause)

def processDeletion(targetLayer, whereclause):
        targetFeature = targetLayer
        recordstoDelete = whereclause
       
        gis_payload = {
            'token': portaltoken,
            'f': 'json',
            'where': recordstoDelete
            }
        response = requests.request("POST", url=targetFeature , data=gis_payload)

 

1 Solution

Accepted Solutions
JosephRhodes2
Occasional Contributor II

This should work for you, or hopefully put you on the right track. The code below will delete ALL features in your service in 2000-record chunks, unless you modify the where_clause variable to specify only certain features. Use with caution.

You may also want to look at Truncate (Feature Layer)—ArcGIS REST APIs | ArcGIS Developers if you are truly trying to delete every feature.

import requests

feature_layer_url = "https://<your_server_url>/arcgis/rest/services/<some_feature_service>/FeatureServer/0"

where_clause = "1=1" # this will grab all features, or you can replace with a different where clause. The objectid chunks will be handled later.
batch_size = 2000

while True:
    query_url = f"{feature_layer_url}/query?where={where_clause}&returnIdsOnly=true&f=json&token={portaltoken}"
    query_result = requests.get(query_url).json()

    object_ids = query_result.get("objectIds", [])
    if not object_ids:
        break  # No more features to delete

    for i in range(0, len(object_ids), batch_size):
        batch_ids = object_ids[i:i + batch_size]
        where_clause_with_ids = f"{where_clause} AND OBJECTID >= {min(batch_ids)} AND OBJECTID <= {max(batch_ids)}"
        
        delete_params = {
            'where': where_clause_with_ids,
            'f': 'json',
            'rollbackOnFailure': True,
            'token': portaltoken
        }

        response = requests.post(f"{feature_layer_url}/deleteFeatures", data=delete_params)

        delete_result = response.json()
        if 'deleteResults' in delete_result:
            deleted_features = delete_result['deleteResults']
            for feature in deleted_features:
                if 'success' in feature and feature['success']:
                    print(f"Deleted feature with objectId {feature['objectId']}")
                else:
                    print(f"Failed to delete feature with objectId {feature['objectId']}")
        else:
            print(f"Error deleting features. Response: {delete_result}")

 

View solution in original post

0 Kudos
1 Reply
JosephRhodes2
Occasional Contributor II

This should work for you, or hopefully put you on the right track. The code below will delete ALL features in your service in 2000-record chunks, unless you modify the where_clause variable to specify only certain features. Use with caution.

You may also want to look at Truncate (Feature Layer)—ArcGIS REST APIs | ArcGIS Developers if you are truly trying to delete every feature.

import requests

feature_layer_url = "https://<your_server_url>/arcgis/rest/services/<some_feature_service>/FeatureServer/0"

where_clause = "1=1" # this will grab all features, or you can replace with a different where clause. The objectid chunks will be handled later.
batch_size = 2000

while True:
    query_url = f"{feature_layer_url}/query?where={where_clause}&returnIdsOnly=true&f=json&token={portaltoken}"
    query_result = requests.get(query_url).json()

    object_ids = query_result.get("objectIds", [])
    if not object_ids:
        break  # No more features to delete

    for i in range(0, len(object_ids), batch_size):
        batch_ids = object_ids[i:i + batch_size]
        where_clause_with_ids = f"{where_clause} AND OBJECTID >= {min(batch_ids)} AND OBJECTID <= {max(batch_ids)}"
        
        delete_params = {
            'where': where_clause_with_ids,
            'f': 'json',
            'rollbackOnFailure': True,
            'token': portaltoken
        }

        response = requests.post(f"{feature_layer_url}/deleteFeatures", data=delete_params)

        delete_result = response.json()
        if 'deleteResults' in delete_result:
            deleted_features = delete_result['deleteResults']
            for feature in deleted_features:
                if 'success' in feature and feature['success']:
                    print(f"Deleted feature with objectId {feature['objectId']}")
                else:
                    print(f"Failed to delete feature with objectId {feature['objectId']}")
        else:
            print(f"Error deleting features. Response: {delete_result}")

 

0 Kudos