HOW TO
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.
Delete multiple items by specifying the item IDs of the items to be deleted
import arcgis from arcgis.gis import GIS
#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')
items = ['itemID1', 'itemID2', 'itemID3', 'itemID4', 'itemID5']
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
import arcgis from arcgis.gis import GIS
#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')
shapefile_list = gis.content.search('', item_type='Shapefile', max_items=-1)
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
import arcgis from arcgis.gis import GIS
#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')
shapefile_list = gis.content.search(query="owner:username", max_items=-1)
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
import arcgis from arcgis.gis import GIS
#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')
me = gis.users.me print (me.folders)
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')
Get help from ArcGIS experts
Download the Esri Support App