Batch delete items using ArcGIS Python API

826
4
Jump to solution
03-12-2021 04:38 PM
Labels (2)
RachelBu
New Contributor II

I'm trying to batch delete contents in ArcGIS Online. For example, when users export a feature service to File Geodatabase, there will be a geodatabase object in ArcGIS online before downloading to local folders. I wonder if I can write a script to delete all the file geodatabase objects. I know how to delete a single item using the API like item.delete(). But I don't know how to delete items in a bulk.

I appreciate your help!

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Notable Contributor

There is a function delete_items() that is part of the ContentManager class. Use it like this:

 

gis = arcgis.GIS('url', 'user', 'pw')

fgdb_list = gis.content.search('', item_type='File Geodatabase', max_items=-1)

gis.content.delete_items(fgdb_list)

 

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
jcarlson
MVP Notable Contributor

It's actually part of the query string, details of which you can find here.

fgdb_list = gis.content.search(query="owner:my-username", item_type='File Geodatabase', max_items=-1)
- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
4 Replies
jcarlson
MVP Notable Contributor

There is a function delete_items() that is part of the ContentManager class. Use it like this:

 

gis = arcgis.GIS('url', 'user', 'pw')

fgdb_list = gis.content.search('', item_type='File Geodatabase', max_items=-1)

gis.content.delete_items(fgdb_list)

 

 

- Josh Carlson
Kendall County GIS
0 Kudos
RachelBu
New Contributor II

If I want to delete item created by me only. How do I set the owner parameter in the script? The search function doesn't seem to have a parameter like item owner. 

 

0 Kudos
jcarlson
MVP Notable Contributor

It's actually part of the query string, details of which you can find here.

fgdb_list = gis.content.search(query="owner:my-username", item_type='File Geodatabase', max_items=-1)
- Josh Carlson
Kendall County GIS
0 Kudos
RachelBu
New Contributor II

Thank you so much, Josh!