Saturday, June 1, 2013

Google Apps Bulk Delete Users Script

Google Apps has a bulk account creation tool, but nothing for bulk deleting accounts. I had previously written something in Python to bulk delete accounts from a text file, but it was time to create something web-based. This will only work if you have the Domain API enabled, which means that you'll have to enable it in your Google Apps for Education Admin console.

Just a note, though, before deleting users you may want to direct them to the Data Liberation tools for downloading their data.

The actual script is in Google Apps Script. You can see it by visiting Google Apps Delete Users, or create your own copy at script.google.com using the source code below.

Let me know if this works for you, or if you have suggestions for improvements.


 function doGet() {  
 // get the user's credentials for their Google Apps account  
  var user = Session.getEffectiveUser().getUserLoginId()  
  var domain = UserManager.getDomain();  
  var welcome = "You are running this script as " + user + " on the domain " + domain;  
 // set up the user interface  
  var app = UiApp.createApplication().setTitle('Delete Google Apps Users by MisterHay');  
  app.add(app.createLabel(welcome));  
  app.add(app.createHTML("<br>Make sure you have enabled the Provisioning API (support.google.com/a/bin/answer.py?hl=en&answer=60757).<br>This script will delete Google Apps user accounts that you paste or type below. Each account name must be on its own line.<br>e.g.<br>misterhay<br>mpython<br>unladen.swallow<p>"));  
  var textArea = app.createTextArea().setName("textArea").setSize("20%", "60%").setStyleAttribute("background", "white").setStyleAttribute("color", "black").setFocus(true);  
  var serverHandler = app.createServerHandler("deleteAccounts").addCallbackElement(textArea);  
  var clientHandler = app.createClientHandler().forEventSource().setEnabled(false).setText("deleting accounts...");  
  app.add(textArea);  
  var button = app.createButton("Delete Accounts");  
  button.addClickHandler(serverHandler);  
  button.addClickHandler(clientHandler);  
  app.add(button);  
  app.add(app.createLabel("no accounts deleted").setId("finishedLabel").setVisible(false));  
  return app;  
 }  
   
 function deleteAccounts(eventInfo) {  
  var app = UiApp.createApplication();  
  var deleteThese = eventInfo.parameter.textArea;  
  var stringArray = deleteThese.split(/\n/);  
  for (var loopNumber = 0; loopNumber < stringArray.length; loopNumber++) {  
   var deleteThisUser = stringArray[loopNumber];  
 // rename the accounts before we delete them to avoid the five day wait before account names can be reused  
   var renamedUser = deleteThisUser + '_old';  
   Logger.log(renamedUser);  
 // delete the account  
   UserManager.getUser(deleteThisUser).setUsername(renamedUser);  
   UserManager.getUser(renamedUser).deleteUser();  
   }  
 // tell the user how many accounts we deleted.  
  app.getElementById("finishedLabel").setText(loopNumber + " accounts deleted").setVisible(true);  
  return app;  
 }  

12 comments:

J Kellett said...

Many thanks for this, it saved a huge amount of time when deleting 200+ school leavers.

I'm sure it used to be possible to multi-select users and delete within the Admin Console? However, I can't see how to do this any more using the dreadful new Apps interface, so your script is very welcome!

Unknown said...

Hi. I dont really know how to get it work. Can I please have more instructions?
regards Kjelle

David Hay said...

@Kjell TYR - Bengtsson Hopefully you should be able to run the script from the link in the post. It will ask you for authorization, make sure you're logged in to an account with administrative permissions on the domain that you're trying to delete users from.

After that, it should be just a matter of typing or pasting a list of accounts into the text box, and clicking the button to delete them.

jegan said...

David,

Thanks for making the script and publicly sharing it!

When I copy and paste my list of usernames to delete I get an error:
http://imgur.com/LBb9FW5

If I manually type in the username or copy and paste it from the username list on the script it works fine.

You can see in the image that abates.student will not delete but the same exact username shows up on the username list.

I typed in abates.student manually and it deleted fine.

Thanks for your help,
Justin

David Hay said...

@jegan

Yeah, I haven't included any sort of logging or error handling. The issue might be that you have a space before or after the username.

Is it always the first username that give you an error? Perhaps try pasting the list into a text editor (such as Notepad) to make sure there's nothing extra that's getting pasted in.

Of course it's not inconceivable that there's a bug in the code. Feel free to make a copy and debug it :)

jegan said...

I did that. Thanks.

Mrs H said...

When running the script I get the following message

You do not have permission to call getEffectiveUser (line 3, file "Code", project "Google Apps Bulk Delete Users")

Any help is much appreciated.

David Hay said...

@Mrs H Not sure what that error message is about, maybe something the API has changed. Are you running the script from the link in the post or have you created your own copy?

It may be easier at this point to use a more recent and powerful tool such as GAM (https://github.com/jay0lee/GAM/tree/master).

Unknown said...

You can try to use this... https://mygapps.tools

Unknown said...

I have developed a web application tool that might be helpful.
I have tried it with over 5000 users and it worked.
Go here http://mygapps.tools

Mrs H said...

Worked well. Easy to understand and saved a great deal of time. Thank you.

Henrique said...

work fine, very good.
good job!!!