<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Grab sections of Service to process in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/grab-sections-of-service-to-process/m-p/1372556#M9552</link>
    <description>&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You may also want to look at&amp;nbsp;&lt;A href="https://developers.arcgis.com/rest/services-reference/online/truncate-feature-layer-.htm" target="_blank"&gt;Truncate (Feature Layer)—ArcGIS REST APIs | ArcGIS Developers&lt;/A&gt;&amp;nbsp;if you are truly trying to delete every feature.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

feature_layer_url = "https://&amp;lt;your_server_url&amp;gt;/arcgis/rest/services/&amp;lt;some_feature_service&amp;gt;/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}&amp;amp;returnIdsOnly=true&amp;amp;f=json&amp;amp;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 &amp;gt;= {min(batch_ids)} AND OBJECTID &amp;lt;= {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}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jan 2024 21:47:12 GMT</pubDate>
    <dc:creator>JRhodes</dc:creator>
    <dc:date>2024-01-19T21:47:12Z</dc:date>
    <item>
      <title>Grab sections of Service to process</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/grab-sections-of-service-to-process/m-p/1372510#M9551</link>
      <description>&lt;P&gt;I am trying to delete records via REST Service.&amp;nbsp; the smaller tables delete no issues but Feature Classes with images seem to NOT delete as they have more than 10,000 records&lt;/P&gt;&lt;P&gt;If I look at the ObjectIDs and grab the first half and then the second half they both delete.&lt;BR /&gt;&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;Anyone have any ideas?&lt;/P&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;How can I grab my features in CHUCKS&lt;/P&gt;&lt;LI-CODE lang="c"&gt;if targetLayer == "GIS_DATA.SDEInventory":
    whereclause="{}".format("OBJECTID&amp;lt;15000")
    processDeletion(url, whereclause)
    whereclause="{}".format("OBJECTID&amp;gt;=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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 20:30:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/grab-sections-of-service-to-process/m-p/1372510#M9551</guid>
      <dc:creator>kapalczynski</dc:creator>
      <dc:date>2024-01-19T20:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Grab sections of Service to process</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/grab-sections-of-service-to-process/m-p/1372556#M9552</link>
      <description>&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You may also want to look at&amp;nbsp;&lt;A href="https://developers.arcgis.com/rest/services-reference/online/truncate-feature-layer-.htm" target="_blank"&gt;Truncate (Feature Layer)—ArcGIS REST APIs | ArcGIS Developers&lt;/A&gt;&amp;nbsp;if you are truly trying to delete every feature.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

feature_layer_url = "https://&amp;lt;your_server_url&amp;gt;/arcgis/rest/services/&amp;lt;some_feature_service&amp;gt;/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}&amp;amp;returnIdsOnly=true&amp;amp;f=json&amp;amp;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 &amp;gt;= {min(batch_ids)} AND OBJECTID &amp;lt;= {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}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 21:47:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/grab-sections-of-service-to-process/m-p/1372556#M9552</guid>
      <dc:creator>JRhodes</dc:creator>
      <dc:date>2024-01-19T21:47:12Z</dc:date>
    </item>
  </channel>
</rss>

