Cloud API Resources

Read quickstart guides and try out example API scripts.

Remove Users in Bulk from a Workspace using the AAC API

briancoombs
Alteryx
Alteryx
Created

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…

  1. Install the Python version library by running this command:

    1. python --version
  2. Install the Python library to make API calls in Python "Requests":

    1. python -m pip install requests
    2. If this doesn’t work, you may need to install pip.

  3. Save this Python script using a name of your choice (for example, removeAllUsersExceptExcluded.py):

    1. 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"))
  4. Edit removeAllUsersExceptExcluded.py to match your workspace and needs.

    1. 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-....

    2. 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 ([ ])/

    3. Save your changes.

  5. Run your script with this command:

    1. python removeAllUsersExceptExcluded.py

We’ve completed these steps to address the use case of needing to remove users in bulk…

  1. Created an API token from our workspace and created a refresh flow.

  2. Removed all users except a select few from a workspace as needed.

We can improve on this small project by adding

  1. Connections to our AD groups to automatically remove certain users who have left the company.

  2. 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.

  3. 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!