"Remove all people from this group"

Hello,

We’re looking to scrip/automate the below action via API call, but cannot seem to find the appropriate endpoint/action to call. Is it something that exists? We’d like to avoid having to export the user list to a .csv and iterate through it line by line. Thanks.

Hello,

From the API if scripting typically you would get all the users assigned to a group,

Then iterate through and remove each one,

Thank You,

2 Likes
import axios from 'axios';


const oktaApiUrl = 'https://dev-xxxx.okta.com';
const apiKey = 'xxxx'; // Use environment variables for sensitive data
const groupId = '00g2444n5gRcr3wYT357';

// Get all users in the group
async function getGroupMembers() {
    try {
      const response = await axios.get(`${oktaApiUrl}/api/v1/groups/${groupId}/users`, {
        headers: {
          Authorization: `SSWS ${apiKey}`,
        },
      });
  
      return response.data;
    } catch (error) {
      console.error('Error getting group members:', error.response ? error.response.data : error.message);
      throw error;
    }
  }
  
  // Remove a user from the group
  async function removeUserFromGroup(userId) {
    try {
      await axios.delete(`${oktaApiUrl}/api/v1/groups/${groupId}/users/${userId}`, {
        headers: {
          Authorization: `SSWS ${apiKey}`,
        },
      });
      console.log(`User ${userId} removed from group.`);
    } catch (error) {
      console.error(`Error removing user ${userId} from group:`, error.response ? error.response.data : error.message);
      throw error;
    }
  }
  
  // Main script
  (async () => {
    try {
      const groupMembers = await getGroupMembers();
      
      for (const member of groupMembers) {
        //console.log(member.id);
        await removeUserFromGroup(member.id);
      }
  
      console.log('All users removed from the group.');
    } catch (error) {
      console.error('Script error:', error);
    }
  })();


The above code will work for you.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.