Automatically archive users based on Org Unit membership

Automatically assign Google Workspace Archive User license based on Org Unit membership

You might be familiar with automatic license assignment feature in Google Workspace, all you have to do there is to select your organization unit and assign a specific license.

Then onwards, once a new user is either created or moved in that Org Unit, Google will automatically assign license to him/her.

Above feature is great, but unfortunately it does not work with “Google Workspace Archive User SKU” :(.

However, we can leverage Google Apps Script to automate it, essentially our workflow will look like this-:

1. Get a list of all the users in our required Org Unit (e.g Archived_Users).
2. Search for unarchived users in this list.
3. Archive all users who are active.
4. Create a trigger (cron job) to run a function which does #1,2 and 3.

Here is a flow diagram to understand it better-:

Google Workspace Archive User Script Flow

Copy the following Google Apps Script CodeCopy the following script and follow the video to see how we can automate archive user license assignment based on Org Unit membership.


function archiveUsers() {

// Get All users from Archived_Users Org. Unit, Please change the org unit path to yours.

   var pageToken;
   do {
       var page = AdminDirectory.Users.list({
           customer: "my_customer",
           query: 'orgUnitPath=/Archived_Users',
           maxResults: 100,
           pageToken: pageToken

       var archiveOuUsers = page.users

// Find the users who are not yet archived

       for (i = 0; i < archiveOuUsers.length; i++) {

           if (archiveOuUsers[i].archived == false) {
               var userToBeArchived = archiveOuUsers[i].primaryEmail

// Archive the users who are still active.

               try {

                   var archivalStatus = AdminDirectory.Users.patch({
                       archived: true
                   }, userToBeArchived)

               } catch (e) {


       pageToken = page.nextPageToken;

// Run the script till the time we have next set of users available as per pageToken

   } while (pageToken);


Related Posts