HOW TO

Batch delete ArcGIS Online or Portal for ArcGIS items using ArcGIS API for Python

Last Published: October 6, 2022

Summary

ArcGIS Online and Portal for ArcGIS allow batch deleting items programmatically using the delete_items() or delete_folder() function in the arcgis module through ArcGIS API for Python. This article provides workarounds to perform the batch deletion of features using Python.

Procedure

Delete multiple items by specifying the item IDs of the items to be deleted

  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
  1. Connect to the account and specify the credentials.
#For ArcGIS Online
gis = GIS('https://arcgis.com', 'username', 'password')

#For Portal for ArcGIS
gis = GIS('https://<machine>.<domain>/<web adaptor name>/home', 'username', 'password')

print ('Connected')
  1. Specify the items IDs to be deleted.
items = ['itemID1', 'itemID2', 'itemID3', 'itemID4', 'itemID5']
  1. Call the delete_items() function.
gis.content.delete_items(items)
print('Deleted')

The code block below shows the full script for ArcGIS Online.

import arcgis
from arcgis.gis import GIS

gis = GIS('https://arcgis.com', 'username', 'password')
print ('Connected')

items = ['fcasfcaef', 'awsfef23f', 'asvqgf2324', 'asfceqwr23', 'asdf324r24']

gis.content.delete_items(items)
print('Deleted')

Delete all the items based on the file type

  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
  1. Connect to the account and specify the credentials.
#For ArcGIS Online
gis = GIS('https://arcgis.com', 'username', 'password')

#For Portal for ArcGIS
gis = GIS('https://<machine>.<domain>/<web adaptor name>/home', 'username', 'password')

print ('Connected')
  1. List all the available items based on file type. In this example, the file type used is Shapefile.
shapefile_list = gis.content.search('', item_type='Shapefile', max_items=-1)
  1. Call the delete_items() function.
gis.content.delete_items(shapefile_list)
print('Deleted')

The code block below shows the full script for Portal for ArcGIS.

import arcgis
from arcgis.gis import GIS

gis = GIS('https://machine.esri.com/portal', 'username', 'password')
print ('Connected')

shapefile_list = gis.content.search('', item_type='Shapefile', max_items=-1)

gis.content.delete_items(shapefile_list)
print('Deleted')

Delete all the items based on the username

  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
  1. Connect to the account and specify the credentials.
#For ArcGIS Online
gis = GIS('https://arcgis.com', 'username', 'password')

#For Portal for ArcGIS
gis = GIS('https://<machine>.<domain>/<web adaptor name>/home', 'username', 'password')

print ('Connected')
  1. List all the available items based on the username.
shapefile_list = gis.content.search(query="owner:username", max_items=-1)
  1. Call the delete_items() function.
gis.content.delete_items(shapefile_list)
print('Deleted')

The code block below shows the full script for ArcGIS Online.

import arcgis from arcgis.gis import GIS 

gis = GIS('https://arcgis.com', 'username', 'password') 
print ('Connected')

item_list = gis.content.search(query="owner:username", max_items=-1)

gis.content.delete_items(item_list)
print('Deleted')

Delete all the items in a specific folder by deleting the folder

  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
  1. Connect to the account and specify the credentials.
#For ArcGIS Online
gis = GIS('https://arcgis.com', 'username', 'password')

#For Portal for ArcGIS
gis = GIS('https://<machine>.<domain>/<web adaptor name>/home', 'username', 'password')

print ('Connected')
  1. List all the available folders based on the account.
me = gis.users.me
print (me.folders)
  1. Call the delete_folder() function.
gis.content.delete_folder("Folder Name", owner= "username")
print('Deleted')

The code block below shows the full script for Portal for ArcGIS.

import arcgis
from arcgis.gis import GIS

gis = GIS('https://machine.esri.com/portal', 'username', 'password')
print ('Connected')

me = gis.users.me
print (me.folders)

gis.content.delete_folder("Folder Name", owner= "username")
print('Deleted')

Article ID: 000028381

Software:
  • ArcGIS Online
  • Portal for ArcGIS

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options