We have several emergency operation layers that we want to clear out quickly before an event. We will use juniper to share it with our staff too. Thank you.
Although this is not a full recipe, here is the snippet:
You can delete features by calling delete_features() method on your FeatureLayer object like shown below:
feature_layer_item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"your search criteria"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> flayers <SPAN class="operator token">=</SPAN> feature_layer_item<SPAN class="punctuation token">.</SPAN>layers flayer <SPAN class="operator token">=</SPAN> flayers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> flayer<SPAN class="punctuation token">.</SPAN>delete_features<SPAN class="punctuation token">(</SPAN>where<SPAN class="operator token">=</SPAN><SPAN class="string token">"objectid > 0"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
You can specify any other query to selectively delete some features. Alternately, if you want to delete call, then call the truncate() method on the FeatureLayerManager
flayer<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>truncate<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Truncate will delete all features and reset the objectid or fid count back to 0. Help for truncate
Hi All.Does anyone have any guide if I want to delete using multiple clauses? Like for example, I want to delete rows where column A is not 'abc' and column B is not 'def'.Thank you.
Thanks that worked perfectly for me, the only change I had to make was to change OBJECTID to OID because it was throwing up an error.
max_objid = flayer.query(out_statistics=[{"statisticType":"MAX","onStatisticField":"OID","outStatisticFieldName":"MAX_OBJ"}], return_geometry=False)maxoid = max_objid.features[0].attributes['MAX_OBJ']
#delete in steps of 20000 or more, in case the dataset is large
i = 0step = 20000
#replace maxoid with count if attempting to delete features based on feature countwhile i <= maxoid: i += step flayer.delete_features(where=f"OID<= {i}") print(i)
Cheers
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://site.maps.arcgis.com"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"username"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"password"</SPAN><SPAN class="punctuation token">)</SPAN> feature_layer_item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"<item-id>"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> flayers <SPAN class="operator token">=</SPAN> feature_layer_item<SPAN class="punctuation token">.</SPAN>layers flayer <SPAN class="operator token">=</SPAN> flayers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#count = flayer.query(return_count_only=true)</SPAN> max_objid <SPAN class="operator token">=</SPAN> flayer<SPAN class="punctuation token">.</SPAN>query<SPAN class="punctuation token">(</SPAN>out_statistics<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"statisticType"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"MAX"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"onStatisticField"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"OBJECTID"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"outStatisticFieldName"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"MAX_OBJ"</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> return_geometry<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN> maxoid <SPAN class="operator token">=</SPAN> max_objid<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>attributes<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'MAX_OBJ'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#delete in steps of 20000 or more, in case the dataset is large</SPAN> i <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> step <SPAN class="operator token">=</SPAN> <SPAN class="number token">20000</SPAN> <SPAN class="comment token">#replace maxoid with count if attempting to delete features based on feature count</SPAN> <SPAN class="keyword token">while</SPAN> i <SPAN class="operator token"><=</SPAN> maxoid<SPAN class="punctuation token">:</SPAN> i <SPAN class="operator token">+=</SPAN> step flayer<SPAN class="punctuation token">.</SPAN>delete_features<SPAN class="punctuation token">(</SPAN>where<SPAN class="operator token">=</SPAN>f<SPAN class="string token">"OBJECTID <= {i}"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#if using for loop</SPAN> <SPAN class="comment token">#for i in range(1,20000,maxoid):</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Feature Layer was truncated"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Christopher Schreibescript in-case there are other dependencies in your feature layer like sync capability and relationships.
ArcGIS Feature Layer query out_statistics
Best,
Imtiaz
Hi all!
I used the sample that Atma Mani provided and created a small script to delete features from a Hosted Feature Service in AGOL that has multiple sub-layers.
<SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> json <SPAN class="keyword token">import</SPAN> arcgis <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">import</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features layerName <SPAN class="operator token">=</SPAN> <SPAN class="string token">'title:Your Layer Name'</SPAN> maxLayerAmount <SPAN class="operator token">=</SPAN> <SPAN class="number token">10</SPAN> <SPAN class="comment token"># number of sublayers</SPAN> agolURL <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2F" target="_blank">https://</A><SPAN><your org>.maps.arcgis.com"</SPAN></SPAN> agolUN <SPAN class="operator token">=</SPAN> <SPAN class="string token">"<Your UserName>"</SPAN> agolPass <SPAN class="operator token">=</SPAN> <SPAN class="string token">"<Your Password>"</SPAN> <SPAN class="comment token"># connect to your GIS</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>agolURL<SPAN class="punctuation token">,</SPAN> agolUN<SPAN class="punctuation token">,</SPAN> agolPass<SPAN class="punctuation token">)</SPAN> layerCount <SPAN class="operator token">=</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN>layerCount <SPAN class="operator token"><</SPAN> maxLayerAmount<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> layerCount <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> feature_layer_item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>layerName<SPAN class="punctuation token">,</SPAN> item_type <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Feature Service'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> flayers <SPAN class="operator token">=</SPAN> feature_layer_item<SPAN class="punctuation token">.</SPAN>layers flayer <SPAN class="operator token">=</SPAN> flayers<SPAN class="punctuation token">[</SPAN>layerCount<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layer ID: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>layerCount<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" Layer Name: "</SPAN> <SPAN class="operator token">+</SPAN> flayer<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN> flayer<SPAN class="punctuation token">.</SPAN>delete_features<SPAN class="punctuation token">(</SPAN>where<SPAN class="operator token">=</SPAN><SPAN class="string token">"objectid > 0"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I also added some logging to display the name of the layers as it loops through them.
The logging looks something like this:
Layer ID: 0 Layer Name: Point Layer Layer ID: 1 Layer Name: Line Layer Layer ID: 2 Layer Name: Polygon Layer<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I am still new to Python, so if there are any suggestions/comments, please let me know!
Hope this helps!
Chris
Hi Amber Lauzon ,
Could you mark Atma Mani 's post as the correct answer please? This will help other users to quickly find the correct solution.
Thanks!
One more caveat: looks like truncate doesn't work if sync is enabled on the dataset.
Not much of a problem though as the delete features seems to work fine and reasonably quickly.
flayer<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>delete_features<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">(</SPAN>where<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN><SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">"objectid > 0"</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">)</SPAN>
Pardon my inexperience, but I am trying to install the py for api package and I cannot using ArcGIS Pro 1.4, do you have any suggestions? The "Updates" and "Add" tabs are blank too...
One caveat, Truncate is not yet supported on ArcGIS Enterprise, it works for ArcGIS Online though.
Thank you! Just what I needed!
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.