Auto Unassign Google Workspace License from your users

Reference script to un-assign Google Workspace licenses from users:

------------------------------------------------------------------------

function removeLicense() {

   // You can also tweak the script to rather run it based on user suspention staus (by checking user.suspended property from Directory API) instead of OrgUnit membership

   // Please be careful before running this script, if run, it'll remove the license from your Google Workspace user.

   // I just created this for a quick reference, so feel free to polish it as required.

   // You would need to provide Google Workspace product name and SKU id which is currently assigned to suspended user or you can first make a get call to read license information for the user and then make delete call with it.
   // You can get this information from License Manager API documentation here
   // https://developers.google.com/admin-sdk/directory/v1/reference/users
   // I will be using Google Workspace Enterprise Product Name and SKU ID as my suspended users are assigned Google Workspace enterprise license

   //-----------------------------------//

   // Step 1. Get All users from suspended_users Organization Unit, Please change the org unit path to yours.


   var productId = "Google-Apps"
   var skuId = 1010020020

   var pageToken;
   do {
       var suspendedOu = AdminDirectory.Users.list({
           customer: "my_customer",
           query: 'orgUnitPath=/suspended_users', // change OU path here as required
           maxResults: 100,
           pageToken: pageToken
       })

       var suspendedOuUsers = suspendedOu.users


       // Step 2. (Optional but recommended) Make sure to only suspend users from this OU who are in suspended state by checking their suspension status

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

           if (suspendedOuUsers[i].suspended == true) {
               var suspendedUserEmail = suspendedOuUsers[i].primaryEmail


               // Step 3. Delete the license from users in our above defined suspended OU who are also in suspended user state.

               try {
                   var licenseRemovalStatus = AdminLicenseManager.LicenseAssignments.remove(productId, skuId, suspendedUserEmail)
               } catch (e) {
                   Logger.log(e.message)
               }

           }

       }
       pageToken = suspendedOu.nextPageToken;


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

   } while (pageToken);
}

------------------------------------------------------------------------

Related Posts

....

....