Bulk Removing Users From G Suite Admin


Bulk Remove G Suite Users

I am the apps admin on my G Suite EDU domain. For a training I had created 100 temporary accounts and then wanted to delete them after the training. Doing a Google Search the results show me how to tediously remove one user at a time. Unacceptable. It took me probably longer to find how to use Google Apps Script to remove multiple users at once than it would have to just remove each individual user but I found some sample code on stack overflow and was able to quickly remove the users.

Download Users

After going to admin.google.com and clicking on Users I was able to click on “Download users.”

Download Users in Admin Panel

The default is to download only the 5 columns shown in the admin panel, and that is probably all I needed but I like seeing what all data it showed for each user. And OF COURSE I want to export it to a Google Sheets spreadsheet.
download user data to google sheets

Sort Spreadsheet

It is a beautiful thing to be able to sort a spreadsheet of user data. I sorted by “Last Sign In [READ ONLY]” to see who had never signed in or had signed in only yesterday. I deleted the rows that included myself and others on my domain who actually used their accounts yesterday. I also deleted the rows that are active but not used yesterday. Leaving me with a spreadsheet of data of users I wanted to delete. I ended up with 1654 users I wanted to delete. I renamed the tab to “neverloggedin.”

Google Apps Script

Going to script.google.com I wanted to code removing all 1654 users at once. I had used this nifty page from the Google Developers page to remove a few one at a time (but still faster than the admin panel), so my actual number was slightly lower. This is important because when I tried to run my script I got an error since some of the users I was trying to remove did not exist.

READ ALSO  The technology behind OpenAI’s fiction-writing, fake-news-spewing AI, explained

Authorize the API

Use the Resources menu to select “Advanced Google services.”

Resources Menu advanced google services

and enable the Admin Directory API.

Admin API

Write the Code

It’s ridiculous how easy this is to code and how hard it was to find how easy it was to code. Thanks to Stacks Overflow for the solution. After enabling the API for Admin directory you simply need AdminDirectory and press period to manage the users on your G Suite domain!

code for delete users

Link to script file

function deleteUsers() {
var ss = SpreadsheetApp.openById(’14g2QfLJkEGHhS1PNyasLQe_JlKP1a36tqhWjN4irFE’);//id of exported spreadsheet
var sheet = ss.getSheetByName(‘neverloggedin’);//tab name of data
var data = sheet.getDataRange().getValues();//all the data in an array

var len = data.length;//number of rows in the array

for(var i=1; i<len; i++){//has a header row, start counting at 1
var user = data[i][2];//3rd column has the user email addresses
Logger.log(user);

//use try catch in case a user is already removed
try{
AdminDirectory.Users.remove(user);
}
catch(err){}

}
}

 

 

 

 



Source link

?
WP Twitter Auto Publish Powered By : XYZScripts.com