TeamDynamix Admins: Script for Purging Email From a TDX Google Mailbox


TeamDynamix Google Mailboxes


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.


Use this link to check usage while logged into the mailbox:

Apply a script to the TDX Google mailbox to automatically purge emails.

  1. Log into the mailbox using an incognito browser.
  2. In a new tab go to
  3. Click +New Project.
  4. Click on the project name and rename to preferred name (i.e. Purge System Loops).
  5. Delete the provided code, rows 1-3.
  6. Copy sample script into source page. (script below.)
  7. Update LABEL_TO_DELETE to the label you want to run the script. Use the front-end label name. (i.e. System Loops)
  8. Update DELETE_AFTER_DAYS to the number of days after email creation to delete.
  9. Save Project.
  10. In the toolbar, select Initialize (if not already defaulted) > Click Run.  Google will ask you to grant required permissions. Click Review Permissions.
  11. A pop up window will display > Select the mailbox account.
  12. A new pop up window will display > Click on Allow.
  13. Click on the Initialize dropdown and select Install > Click Run.  This will install and start the script for your account.
  14. 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.
  15. 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:2022/11/11

   Plese read post below on know how to use this script.

// The name of the Gmail Label that is to be checked for purging?
var LABEL_TO_DELETE = "System Loops";    

// Purge messages in the above label automatically after how many days?

function Intialize() {

function Install() {

           .at(new Date((new Date()).getTime() + 1000*60*2))


function Uninstall() {
  var triggers = ScriptApp.getProjectTriggers()();
  for (var i=0; i<triggers.length; i++) {

function deleteGmail() {
  var age = new Date();  
  age.setDate(age.getDate() - DELETE_AFTER_DAYS);    
  var purge  = Utilities.formatDate(age, Session.getTimeZone(), "yyyy-MM-dd");
  var search = "label:" + LABEL_TO_DELETE + " before:" + purge;
  try {
    var threads =, 0, 100);
    if (threads.length == 100) {
               .at(new Date((new Date()).getTime() + 1000*60*10))
    for (var i=0; i<threads.length; i++) {
      var messages = GmailApp.getMessagesForThread(threads[i]);
      for (var j=0; j<messages.length; j++) {
        var email = messages[j];       
        if (email.getDate() < age) {
  } catch (e) {}


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.


Article ID: 9671
Tue 2/14/23 12:48 PM
Tue 5/2/23 4:26 PM