Code to remove users from ArcGIS Online

2460
3
Jump to solution
01-31-2020 02:13 PM
JoeWeyl
Occasional Contributor

My company has a training site for the Esri training that is ArcGIS Online based, but isn't attached to our Organization account. One of our developers generated a list of users for me from the site using Python and I have taken that list and compared it to our HR Database to find users who have left the company (over 1000 of them). I have exported that list into a comma separated file by email address. We are using SAML for authentication. Now, what I would like to do is to use Jupyter Notebooks to learn how to properly use the Python API for ArcGIS to remove these users. I am out of touch with writing code so that is why I am trying to relearn using Jupyter.

I reviewed this site for other users who are trying to remove users from ArcGIS Online, but my site is more simple, the accounts only have access to the training, no content. To remove them, I want to connect to the site, import the CSV file I have, loop through the file and delete the users.

This is my code so far, I know it isn't working because the users are still on the site, so I am aware I am missing proper execution.

from arcgis.gis import GIS

import csv

gis = GIS("https://****.maps.arcgis.com", "****", "***")

csvPath = r'Myfile path'

userDelete[]

^^^^^ This code below I copied from another post^^^

with open(csvPath) as f_input:
   csv_input = csv.reader(f_input, delimiter=",")
   for row in csv_input:
      users = gis.users.search("username:{}".format(row[0]))
      if (len(users)>0): # only append if there are search results
         userDelete.append(users[0])

As a novice any help in understanding what I need to do to make this work would be appreciated. I want to be more self sufficient with working with Python like this and since I don't code everyday, that takes a lot of relearning.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
JoshuaWatson1
New Contributor II

Hi Joe Weyl,

This code snippet below should get you in the right direction (I have tried to spread it out and include comments to make it easier to read)...

from arcgis.gis import GIS
import csv

gis = GIS("https://****.maps.arcgis.com", "****", "***")
csv_file_path = r'CSV File Path'

with open(csv_file_path) as csv_file_input:
    csv_input = csv.reader(csv_file_input, delimiter=",")
    for row in csv_input:
        print(str(row[0]))

        # Try to find the User with the specified Name. If found, delete the User
        user_by_name = gis.users.get(str(row[0]))

        if user_by_name:
            print(" - Deleting...")
            user_by_name.delete()
        else:
            # If user could not be found by name; search by email, and delete if a single member is found.
            users_by_email = [user for user in gis.users.search() if user.email == str(row[1])]
            if len(users_by_email) == 1:
                print(" - Deleting...")
                users_by_email[0].delete()
            elif len(users_by_email) > 1:
                print(" - More than one user found with the specified email")
            else:
                print(" - No user found with the name or email specified")

This script will process a CSV that is formatted like below:

Member1,Member1@email.com
Member2,Member2@email.com

This will only work if the User's do not own any Content; it is easy to adjust for handling User Content, so if needed just reply and let me know and I will adjust as necessary.

Hope this helps,

Josh

View solution in original post

3 Replies
JoshuaWatson1
New Contributor II

Hi Joe Weyl,

This code snippet below should get you in the right direction (I have tried to spread it out and include comments to make it easier to read)...

from arcgis.gis import GIS
import csv

gis = GIS("https://****.maps.arcgis.com", "****", "***")
csv_file_path = r'CSV File Path'

with open(csv_file_path) as csv_file_input:
    csv_input = csv.reader(csv_file_input, delimiter=",")
    for row in csv_input:
        print(str(row[0]))

        # Try to find the User with the specified Name. If found, delete the User
        user_by_name = gis.users.get(str(row[0]))

        if user_by_name:
            print(" - Deleting...")
            user_by_name.delete()
        else:
            # If user could not be found by name; search by email, and delete if a single member is found.
            users_by_email = [user for user in gis.users.search() if user.email == str(row[1])]
            if len(users_by_email) == 1:
                print(" - Deleting...")
                users_by_email[0].delete()
            elif len(users_by_email) > 1:
                print(" - More than one user found with the specified email")
            else:
                print(" - No user found with the name or email specified")

This script will process a CSV that is formatted like below:

Member1,Member1@email.com
Member2,Member2@email.com

This will only work if the User's do not own any Content; it is easy to adjust for handling User Content, so if needed just reply and let me know and I will adjust as necessary.

Hope this helps,

Josh

AnttiKajanus
New Contributor III

Also remember if you have set any licenses for the user, you need to revoke them before you can delete the user. 

0 Kudos
JoeWeyl
Occasional Contributor

Hi Josh -

I forgot to respond to this message, but wanted you to know this worked like a charm. I appreciate the help. 

I ran it before (when you responded) and was just running it again when I remembered I hadn't responded. 

Joe

0 Kudos