ArcGIS Online - Script to Delete User Content

772
3
12-04-2021 06:15 AM
mpinney
New Contributor

Hi,

I am an administrator for my organisations AGOL and we are hoping to automate the deletion of content.

We have an excel spreadsheet that lists the organisations users names, the name of their layers, file type, view count etc. We want to use this to filter out specific user content (i.e. low view count/ those that have left the organisation etc) and then automatically delete certain layers.

We are hoping to use a python script to automate this deleting process. Has anyone done this previously?

Example workflow: 

1. Connect to ArcGIS Online

2. For content type (identified in spreadsheet) and content name (identified in spreadsheet) delete

3. Loop to the next layer to delete.

As there are hundreds of these layers that will need to be deleted, any help on how to do this, where to start with the script would be greatly appreciated!

 

0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

We have done something very similar to this. I'm assuming that your spreadsheet includes the itemID of these items? It's much easier if so, but this can be re-written if not. However you get there, you'll need a list of ItemIDs. Here's a general example of the process:

from arcgis.gis import GIS, Item

gis = GIS('your-url', 'user', 'pass')

# If you're using a spreadsheet, this might be where you read the sheet into a list, then only get the itemIDs.
item_id_list = [ ... ]

for i in item_id_list:
    item = Item(gis, i)
    
    # Check if you can delete the item first, then delete
    if item.can_delete:
        item.delete()
    else:
        print(f'{item.title}, id {item.id}, is delete protected.')

 

Alternately, if you're sure that you want to delete these items regardless of potential delete protection, swap this in for the if / else at lines 12 - 15 above:

    if not item.can_delete:
        item.protect(enable=False)

    item.delete()

 

- Josh Carlson
Kendall County GIS
AmandaRing
Esri Contributor

To build on Josh's answer, if you don't already have the itemIDs listed, you can use gis.content.search to search for and access your folders and contents.

You can learn more about using the GIS module to search your contents here.

from arcgis.gis import GIS

gis = GIS('your-url', 'user', 'pass')

#create an empty list that will be filled with items to be deleted
delete_list = []

# search your organization for content that has the desired name and type from your spreadsheet
search_results = gis.content.search('title: {}'.format(content_name), max_items=100)
for result in search_results:
    if result["title"] == content_name and result["type"] == content_type:
            delete_list.append(result)
for item in delete_list:
    # Check if you can delete the item first, then delete
    if item.can_delete:
        item.delete()
    else:
        print('{} is delete protected.'.format(item.title))
Amanda
Product Engineer - Web Analysis
0 Kudos
mpinney
New Contributor

Thanks both, this has been extremely useful. I’ll adapt this to my needs and update with final result for others to use.

0 Kudos