Batch Remove People from Google Groups
This Google Apps Script is for Workspace Admins Only [FREE]
There is a point in every operational plan
where users need to be purged from Google Groups. If you have done this with the default functions it is tedious.
This script will allow you to work with a set of email addresses and group email addresses.
Each email will be checked, and if found removed.
You will get a summary of all the work the script completed to your email.
Remember, this requires top level admin permissions. This is not a shared script or a script on a sheet, this is private.
This post is not an Apps Script tutorial. I have one of those free for subscribers. Meaning, you sign-up for free, and you can find it :). It is call The Primer.
Moving on.
You need this JSON file of permissions:
{
"timeZone": "America/Chicago",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "AdminDirectory",
"version": "directory_v1",
"serviceId": "admin"
},
{
"userSymbol": "Drive",
"version": "v3",
"serviceId": "drive"
},
{
"userSymbol": "AdminGroupsSettings",
"version": "v1",
"serviceId": "groupssettings"
},
{
"userSymbol": "AdminGroupsMigration",
"version": "v1",
"serviceId": "groupsmigration"
},
{
"userSymbol": "Gmail",
"version": "v1",
"serviceId": "gmail"
}
]
},
"oauthScopes": [
"https://www.googleapis.com/auth/admin.directory.group",
"https://www.googleapis.com/auth/admin.directory.group.member",
"https://www.googleapis.com/auth/apps.groups.migration",
"https://www.googleapis.com/auth/apps.groups.settings",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.send_mail"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
Here is the code verbosely commented with sample data.
/**
* removeUsersFromParentGroupsBig
*
* This function goes through a predefined list of user emails and removes each one
* from a list of “parent” Google Groups (e.g., graduating classes). Once it’s done,
* it compiles a summary of who was removed (or not found) and emails that report
* to an administrator.
*/
function removeUsersFromParentGroupsBig() {
// ---------------------------------------------------------------------------
// 1) Define which users we want to remove from the parent groups.
// You can add or remove emails here as needed.
// ---------------------------------------------------------------------------
const emailsToRemove = [
"john.doe@example.org",
"jane.smith@example.org",
"alice.jones@example.org",
"bob.wilson@example.org",
"carol.brown@example.org",
"david.moore@example.org",
"emma.taylor@example.org",
"frank.jackson@example.org",
"grace.white@example.org",
"henry.martin@example.org",
// Add more emails as needed
];
// ---------------------------------------------------------------------------
// 2) Define the list of parent group addresses (e.g., class cohorts).
// Again, feel free to expand this list as your needs grow.
// ---------------------------------------------------------------------------
const parentGroupEmails = [
"class2023@example.org",
"class2024@example.org",
"class2025@example.org",
"class2026@example.org",
"class2027@example.org",
// Add more group emails as needed
];
// ---------------------------------------------------------------------------
// 3) Set up where we’ll send the final report, and prepare an object
// to track which removals actually happened.
// ---------------------------------------------------------------------------
const notifyEmail = "admin.notify@example.org";
const removalSummary = {};
// Initialize each user’s entry in the summary as an empty array.
// Later we’ll push the group names they were removed from.
emailsToRemove.forEach(email => {
removalSummary[email] = [];
});
// ---------------------------------------------------------------------------
// 4) Loop through every group and then every user to attempt removal.
// We use a try/catch so that if a user isn’t in a particular group,
// we handle it gracefully rather than stopping the entire script.
// ---------------------------------------------------------------------------
parentGroupEmails.forEach(groupEmail => {
emailsToRemove.forEach(userEmail => {
try {
// First, check if the user is actually a member of this group.
AdminDirectory.Members.get(groupEmail, userEmail);
// If we made it here, the user exists in the group—so remove them!
AdminDirectory.Members.remove(groupEmail, userEmail);
removalSummary[userEmail].push(groupEmail);
// Log a success message (useful for debugging or execution logs)
Logger.log(`✅ Removed ${userEmail} from ${groupEmail}`);
} catch (e) {
// If the error says “Resource Not Found,” it simply means the user
// wasn’t in that group to begin with.
if (e.message.includes("Resource Not Found")) {
Logger.log(`ℹ️ ${userEmail} not in ${groupEmail}`);
} else {
// For any other errors (permissions, API issues, etc.), log a warning
Logger.log(`⚠️ Error for ${userEmail} in ${groupEmail}: ${e.message}`);
}
}
});
});
// ---------------------------------------------------------------------------
// 5) Build the body of the summary email. We’ll list for each user either
// the groups they were removed from, or note that they weren’t found.
// ---------------------------------------------------------------------------
let body = "Group Cleanup Summary:\n\n";
emailsToRemove.forEach(userEmail => {
const groups = removalSummary[userEmail];
if (groups.length) {
body += `• ${userEmail} removed from:\n ${groups.join("\n ")}\n\n`;
} else {
body += `• ${userEmail} was not found in any parent group.\n\n`;
}
});
// ---------------------------------------------------------------------------
// 6) Send the summary email to the administrator.
// This lets them know exactly what changes were made.
// ---------------------------------------------------------------------------
MailApp.sendEmail({
to: notifyEmail,
subject: "✅ Parent Groups Removal Report",
body: body
});
// Finally, log that we’ve sent the report
Logger.log(`📧 Summary sent to ${notifyEmail}`);
}