Search code examples
google-apps-scriptgoogle-admin-sdkgoogle-directory-api

How can I delete admin role assignments from a user accounts using Google Admin API?


I need to delete role assignments from a list of user accounts. I am familiar with Google Apps Script, but need help working out the script needed to delete the role assignments using the Google Admin API. Has anyone got any examples they could share?


Solution

  • I made a sample code that will get all the roles under that user, and after that it will remove them. Take in consideration that you might need to change it base on your user case.

    function roles_Assignments_1() {
      let user_list = ['[email protected]', '[email protected]', '[email protected]']
      for (let i = 0; i < user_list.length; i++) {
        let roleAssignments_info = AdminDirectory.RoleAssignments
          .list("my_customer", { userKey: user_list[i] });
        try {
          let role_items = roleAssignments_info.items;
          for (let j = 0; j < role_items.length; j++) {
            let roles_Assignments_ID = role_items[j]['roleAssignmentId'];
            AdminDirectory.RoleAssignments.remove('my_customer', roles_Assignments_ID)
          }
        } catch (err) {
          Logger.log("It doesn't have a role")
        }
      }
    }
    

    Reference: