English

How To: Batch remove inactive users in Portal for ArcGIS using ArcGIS API for Python

Summary

Members of an organization can be deleted by users with the privilege to delete members, or by the administrator of the group or organization through the My Organization tab on the Portal for ArcGIS page. This can also be done using the command line utility for bulk remove, such as when it is necessary to delete a large number of members. For more information, refer to the following web help document: Portal for ArcGIS: Manage members, under the 'Delete member' section.

Using ArcGIS API for Python, inactive members can also be removed depending on a specified duration of inactivity. This article describes the steps to do this.

Procedure

  1. Set up ArcGIS API for Python using Conda in the designated machine. For more information, refer to the following web help document: ArcGIS for Developers: ArcGIS API for Python, under the Install using Conda section.
  2. Copy the following code sample and paste into a text editor, for example, Notepad++.
from arcgis.gis import GIS
from IPython.display import display
import os,sys, time

# Loop user to get last login
source = GIS("https://domain.server.com/portal/home", "-------", "--------")

sourceusers = source.users.search(max_users=1000)
sourceusers

ignore_list = ['system_publisher', 'esri_nav', 'esri_livingatlas',
               'esri_boundaries', 'esri_demographics']
for user in sourceusers:
    if not user.username in ignore_list:
        #print(user)

        print(user.username)
        print(user.lastLogin)
	# Specify users inactivity time
        if user.lastLogin == -1:
            print("deleting user: " + user.username)
            #user.delete()
        print("-----------------------")
Note:
Edit the code segment under the comment, Specify users inactivity time, to set the date of last login. The numbers used are the epoch timestamp. The following is a sample code snippet that searches for users last logged in before 4 September 2017, using the epoch timestamp.
if user.lastLogin < 1504487841:
Note:
The Epoch timestamp is the number of seconds that have elapsed since January 1, 1970, excluding leap seconds. 
  1. Save the file as a .py file.

Related Information