Environment
TeamDynamix Google Mailboxes
Issue
The new storage usage limit for shared accounts is set to 15 GB. Emails will need to be purged to the trash to avoid exceeding the storage limit.
Resolution
Use this link to check usage while logged into the mailbox: https://drive.google.com/drive/quota
Apply a script to the TDX Google mailbox to automatically purge emails.
- Log into the mailbox using an incognito browser.
- In a new tab go to https://script.google.com.
- Click +New Project.
- Click on the project name and rename to preferred name (i.e. Purge System Loops).
- Delete the provided code, rows 1-3.
- Copy sample script into source page. (script below.)
- Update LABELS_TO_DELETE to the label you want to run the script. Use the front-end label name. (i.e. System Loop)
- Update DELETE_AFTER_DAYS to the number of days after email creation to delete.
- Save Project.
- In the toolbar, select Initialize (if not already defaulted) > Click Run. Google will ask you to grant required permissions. Click Review Permissions.
- A pop up window will display > Select the mailbox account.
- A new pop up window will display > Click on Allow.
- Click on the Initialize dropdown and select Install > Click Run. This will install and start the script for your account.
- It may take a few minutes for the script to kick off. You can view the Trigger and Execution logs using the icons in the left navigation.
- Create a new project for each label in the mailbox.
The script should automatically run once an email reaches the number of days entered into the script.
Example Script
It may take time for the script to run through all the emails for a label, especially if there are a large number of them. Check the number of emails of each label and ensure the number decreases.
Search the emails filtered on a specific date to ensure they have been deleted. Here’s an example filter:
label:system-loops before:2024/05/24
// The name of the Gmail Label that is to be checked for purging
var LABELS_TO_DELETE = ["Processed"];
var TRIGGER_NAME = "dailyDeleteGmail";
// Purge messages in the above label automatically after how many days?
var DELETE_AFTER_DAYS = "90";
var TIMEZONE = "EST";
// Maximum number of threads to process per run
var PAGE_SIZE = 500;
// If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins)
var RESUME_FREQUENCY = 10;
/*
IMPLEMENTATION
*/
function Intialize() {
return;
}
function Install() {
// First run 2 mins after install
ScriptApp.newTrigger(TRIGGER_NAME)
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
// Run daily there after
ScriptApp.newTrigger(TRIGGER_NAME)
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getProjectTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function dailyDeleteGmail(ev) {
var age = new Date();
age.setDate(age.getDate() - DELETE_AFTER_DAYS);
var purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd");
var search = "(label:" + LABELS_TO_DELETE.join(" OR label:") + ") before:" + purge;
console.log("PURGE: " + purge);
console.log("SEARCH: " + search);
try {
var threads = GmailApp.search(search, 0, PAGE_SIZE);
// Resume again in 10 minutes
if (threads.length == PAGE_SIZE) {
console.log("Scheduling follow up job...");
ScriptApp.newTrigger(TRIGGER_NAME)
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*RESUME_FREQUENCY))
.create();
}
// Move threads/messages which meet age criteria to trash
console.log("Processing " + threads.length + " threads...");
for (var i=0; i<threads.length; i++) {
var thread = threads[i];
if (thread.getLastMessageDate() < age) {
thread.moveToTrash();
} else {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j=0; j<messages.length; j++) {
var email = messages[j];
if (email.getDate() < age) {
email.moveToTrash();
}
}
}
}
} catch (e) {}
/* Delete the trigger that caused this (if there is one), otherwise the trigger
seems to linger in a disabled state. */
if (ev != null) {
if (!ScriptApp.getProjectTriggers().some(function (trigger) {
if (trigger.getUniqueId() === ev.triggerUid) {
ScriptApp.deleteTrigger(trigger);
return true;
}
return false;
})) {
console.error("Could not find trigger with id '%s'", triggerUid);
}
}
}
Additional Information
Need additional information or assistance? Submit a request here ITS-TeamDynamix Support.
Please note that Google Apps Script is not an ITS supported service.