Exact error: Unable to reach the server where the service is. Consider using calling deleteItem with options = { 'force' : true } if the server won't be available anymore.
The issue usually occurs when Portal for ArcGIS is unfederated and re-federated or when some configuration changes are made to Portal for ArcGIS.
<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
gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://portalname/arcgis"</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>
<SPAN class="comment token">#get the list of orphaned items</SPAN>
delete_orphaned_items <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>query<SPAN class="operator token">=</SPAN><SPAN class="string token">"owner: ownername AND title:*title*"</SPAN><SPAN class="punctuation token">,</SPAN> item_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'Feature *'</SPAN><SPAN class="punctuation token">,</SPAN> max_items<SPAN class="operator token">=</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">)</SPAN>
delete_orphaned_items
<SPAN class="comment token">#filter the items </SPAN>
filtered_items <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>item <SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> delete_orphaned_items <SPAN class="keyword token">if</SPAN> <SPAN class="string token">'title'</SPAN> <SPAN class="keyword token">in</SPAN> item<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>filtered_items<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
sorted<SPAN class="punctuation token">(</SPAN>filtered_items<SPAN class="punctuation token">,</SPAN> key<SPAN class="operator token">=</SPAN><SPAN class="keyword token">lambda</SPAN> x<SPAN class="punctuation token">:</SPAN>x<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#replace dry_run with force. dry_run checks if the item can be safely deleted</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> filtered_items<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
item<SPAN class="punctuation token">.</SPAN>delete<SPAN class="punctuation token">(</SPAN>dry_run<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> TypeError<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">'{item.title} not deleted'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> RuntimeError<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">'{item.title} not deleted'</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> Delete an item in Portal for ArcGIS using arcgis.gis module: https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.gis.toc.html#item