How to use usage method with Python API for ArcGIS?

265
2
01-23-2020 06:29 PM
GeoffreyWest
Occasional Contributor III

I want to find all items in my organization that have not been accessed or opened in say the last 6 months and delete them.  I have some code here which gets the item ids, but it appears as if only the first item id is being passed and returning a data frame.  Jake Skinner‌ how can I grab every item id in my list and pass it to the usage method and if that item has no activity then delete it?

Here is the beginning of my code:

from arcgis.gis import GIS, Item
gis = GIS("", "", "")

my_content = gis.content.search(query="owner:" + gis.users.me.username, 
 max_items=15)
for content in my_content:
 item_id = content.id
 item = Item(gis, item_id)
 itemid = item.id
 chardata = item.usage("24H",as_df=True)
 print (chardata)
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Geoffrey,

My understanding is if the data frame is empty, there has been no usage for that layer in the specified time frame.  You could do something like:

from arcgis.gis import GIS

# Variables
sourceUsername = 'jskinner_CountySandbox'
sourcePassword = '******'
sourcePortal = 'https://www.arcgis.com'

# Connect to source portal
source = GIS(sourcePortal, sourceUsername, sourcePassword, verify_cert=False)

# Specify user
source_users = source.users.search('jskinner_CountySandbox')

for user in source_users:
    user_content = user.items()

    # Get items from root folder first
    for item in user_content:
        df = item.usage('7D', as_df=True)
        if len(df) == 0:
           print("\n-------------------------------")
           print(item)
           # Delete item

    # Get item from each of the folders next
    folders = user.folders
    for folder in folders:
        folder_items = user.items(folder=folder['title'])
        for item in folder_items:
            df = item.usage('7D', as_df=True)
            if len(df) == 0:
                print("\n-------------------------------")
                print(item)
                # Delete item
0 Kudos
GeoffreyWest
Occasional Contributor III

Thank you Jake Skinner‌, I will test this out.

0 Kudos