Remove Users in Bulk from a Workspace using the AAC API
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Notify Moderator
on
05-13-2024
10:41 AM
- edited on
06-04-2024
10:40 AM
by
baileykaplan
Overview
A tutorial on using the AAC API for the use case of needing to remove many users from a workspace to clear it out quickly. For example, a SparkEd admin might find this useful when they have no classes of users in their workspace each semester.
Guide
This guide assumes you have limited knowledge of Python, but some research and debugging might be required to set up Python and Pip. Follow these steps…
-
Install the Python version library by running this command:
-
python --version
-
-
Install the Python library to make API calls in Python "Requests":
-
python -m pip install requests
-
If this doesn’t work, you may need to install pip.
-
-
Save this Python script using a name of your choice (for example, removeAllUsersExceptExcluded.py):
-
import requests import logging # Variables to adjust based on the environment BASE_URL = "https://us1.alteryxcloud.com" ACCESS_TOKEN = "YOUR_ACCESS_TOKEN_HERE" EXCLUDED_EMAILS = ["user1_to_exclude@example.com", "user2_to_exclude@example.com"] # Comma-seperated list of emails of users who should not be removed PEOPLE_ENDPOINT = f"{BASE_URL}/v4/people" WORKSPACE_ENDPOINT = f"{BASE_URL}/v4/workspaces/current" # Headers session = requests.Session() session.headers.update({ "Authorization": f"Bearer {ACCESS_TOKEN}", "Accept": "application/json" }) # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def make_request(method, url, **kwargs): try: response = session.request(method, url, **kwargs) response.raise_for_status() # Raise exception for 4xx or 5xx status codes return response.json() except requests.exceptions.RequestException as e: logger.error(f"Request failed: {e}") return None # Function to make a DELETE request for removing a person def remove_person(workspace_id, person_id): url = f"{BASE_URL}/v4/workspaces/{workspace_id}/people/{person_id}" response = make_request("DELETE", url) if response: logger.info(f"Successfully removed person with ID: {person_id}") # Get people data people_response = make_request("GET", PEOPLE_ENDPOINT) # Get current workspace ID workspace_data = make_request("GET", WORKSPACE_ENDPOINT) if workspace_data: workspace_id = workspace_data.get("id") print("Current Workspace ID:", workspace_id) # Ensure we have the people data and the workspace ID if people_response and 'data' in people_response and workspace_id: # Filter out the users with the excluded emails filtered_people = people_response['data'].filter(lambda p: p.get("email") not in EXCLUDED_EMAILS) # Loop through filtered people and remove each for person in filtered_people: remove_person(workspace_id, person.get("id"))
-
-
Edit removeAllUsersExceptExcluded.py to match your workspace and needs.
-
Update the ACCESS_TOKEN variable to be your access token that was created from this guide: https://community.alteryx.com/t5/Alteryx-IO-Resources/How-to-Use-AAC-s-New-OAuth-2-0-Refresh-Tokens-....
-
Edit the excluded_emails array to include all the emails of users you don't want to be removed. If it's just you, make sure there are no commas and enter your email in the brackets ([ ])/
-
Save your changes.
-
-
Run your script with this command:
-
python removeAllUsersExceptExcluded.py
-
Conclusion
We’ve completed these steps to address the use case of needing to remove users in bulk…
-
Created an API token from our workspace and created a refresh flow.
-
Removed all users except a select few from a workspace as needed.
We can improve on this small project by adding…
-
Connections to our AD groups to automatically remove certain users who have left the company.
-
An input interface for less technical users to use this script across many workspaces by just pasting in their access token and the emails they want to protect.
-
A CRON job to run this regularly (for example, on semester end for a university class teaching Alteryx).
Thank you for reading along and if you have issues or questions feel free to comment them below or post in the Developer Discussion Forum!