I am online
← Back to Articles

Gmail: How to Delete Old Emails Automatically

JavaScriptJuly 3, 2024
Gmail: How to Delete Old Emails Automatically

Gmail: How to Delete Old Emails Automatically

Automatically delete old emails with these scripts:

Credits :

Scripts available at GitHub Repository.

Setting Up

  • Create a new Google Apps Script at script.google.com.
  • Overwrite the placeholder with the JavaScript code below.

Script 1: Delete All Old Emails in Promotions Category

const OLD_EMAILS_ON_PROMOSTIONS_CATEGORY = "category:promotions older_than:4m";
const PORMOTION_EMAILS_PAGE_SIZE = 500; // for pagination
function deleteOldCategory() {
  const threads = GmailApp.search(
    OLD_EMAILS_ON_PROMOSTIONS_CATEGORY,
    0,
    PORMOTION_EMAILS_PAGE_SIZE
  );
  for (let i = 0; i < threads.length; i++) {
    const thread = threads[i];
    Logger.log(thread.getFirstMessageSubject());
    threads[i].moveToTrash();
  }
}

This script will get all emails in the Promotions category, delete them in batches of 500, and create a trigger to run the deleteOldPromotionsEmails function daily.

Script 2: Delete Labeled Emails

const LABEL_TO_DELETE = "TO_BE_DELETED";
const DELETE_AFTER_DAYS = 2;
const TIMEZONE = "CET";
const PAGE_SIZE = 150;

function assertThatLabelExist() {
  const labels = GmailApp.getUserLabels();
  const foundedLabels = labels.filter(
    (label) => label.getName() === LABEL_TO_DELETE
  );
  if (foundedLabels.length <= 0) {
    Logger.log(`Would you like to create ${LABEL_TO_DELETE}`);
    throw Error(`${LABEL_TO_DELETE} label not found`);
  }
  Logger.log(`${foundedLabels[0].getName()} label found`);
}

function moveToTrashLabeledEmails() {
  assertThatLabelExist();
  const age = new Date();
  age.setDate(age.getDate() - DELETE_AFTER_DAYS);
  const purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd");
  const searchCriterion = `label:${LABEL_TO_DELETE} AND before:${purge}`;
  Logger.log(searchCriterion);
  const threads = GmailApp.search(searchCriterion, 0, PAGE_SIZE);
  Logger.log(threads.length);
  for (let i = 0; i < threads.length; i++) {
    const thread = threads[i];
    Logger.log(thread.getFirstMessageSubject());
    thread.moveToTrash();
  }
}

This script will get all emails labeled TO_BE_DELETED and delete them in batches of 150. Make sure to create a label TO_BE_DELETED in your Gmail account.

Script 3: Delete All Social Emails from Blacklisted Sources

const LINKED_IN_EMAILS = [
  "invitations@linkedin.com",
  "jobs-listings@linkedin.com",
  "messages-noreply@linkedin.com",
  "jobalerts-noreply@linkedin.com",
  "newsletters-noreply@linkedin.com",
  "messaging-digest-noreply@linkedin.com",
];
const APP_SCRIPT_NOTIFICATION_EMAIL =
  "noreply-apps-scripts-notifications@google.com";

const ALL_BLACKLIST_SOURCE_EMAILS = [
  ...LINKED_IN_EMAILS,
  APP_SCRIPT_NOTIFICATION_EMAIL,
];

const MATCH_MULTIPLE_FORM_TERM = ALL_BLACKLIST_SOURCE_EMAILS.map(
  (fromEmail) => `from:${fromEmail}`
).join(" OR ");

const SOCIAL_CRITERION = "category:social";
const SOCIAL_EMAILS_PAGE_SIZE = 500;
const FINAL_SOCIAL_QUERY = `${SOCIAL_CRITERION} AND (${MATCH_MULTIPLE_FORM_TERM}) AND older_than:5m`;

function deleteAllReceivedEmailFromBlackListedEmails() {
  const threads = GmailApp.search(
    FINAL_SOCIAL_QUERY,
    0,
    SOCIAL_EMAILS_PAGE_SIZE
  );
  for (let i = 0; i < threads.length; i++) {
    const thread = threads[i];
    Logger.log(thread.getFirstMessageSubject());
    threads[i].moveToTrash();
  }
}

This script will get all emails in the Social category from specified blacklisted sources and delete them in batches of 500.

Credits

Scripts available at GitHub Repository.

(Share with others if you found this useful)

Thanks for reading!