Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Created October 7, 2016 13:15
Show Gist options
  • Save DonRichards/0d59ca89b8efdbfe1c0429c258444fe7 to your computer and use it in GitHub Desktop.
Save DonRichards/0d59ca89b8efdbfe1c0429c258444fe7 to your computer and use it in GitHub Desktop.
Gmail: Delete old Emails with specific label

How to delete old emails in Gmail

This script will delete emails old than 30 days with a specified label.

Focussing on emails that are from a mailing list I signed up for. I used Gmails filters to move specific email to a label called "Mailing List". You can set the label to "inbox" if you'd like but this would delete everything in your inbox older than 30 days.

Move emails to a "Mailing List" label

To protect your inbox I suggest moving emails you know are safe to delete to a new label.

  • From your inbox click the checkbox next to the email > More > "Filter Messages Like this"
  • Select how this filter selects the email (either From, Subject, To, etc)
  • Create Filter with this search >>
  • Check these 2
  • Skip the Inbox
  • Apply Label> New Label > Label Name: Mailing List > Create
  • Create Filter

Note: If the label doesn't already exist you'll need to create one with "New Label"

Fun Part! Google Scripts

Go to Google Scripts > Start Scripts

Change "Untitled Project" to anything you'd like
It should give you a default file "Code.gs" with a sample "myFunction".
Lefts replace it with this.

Code.gs
You can change "Mailing List" to any label you are using

function cleanUp() {
  var delayDays = 30 // Enter # of days before messages are moved to trash
 
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays);
   
  var label = GmailApp.getUserLabelByName("Mailing List");
  var threads = label.getThreads();
  for (var i = 0; i < threads.length; i++) {
    if (threads[i].getLastMessageDate()<maxDate)
      {
        threads[i].moveToTrash();
      }
  }
}
  • File Save
  • Run > Cleanup > Authorize the script to access your Gmail account
  • Resources > "Current Project's Triggers"

Current project's triggers

cleanup Events
cleanup Time-driven Day timer Midnight to 1am
  • SAVE

Now let's save and run

  • File Save
  • Run > Cleanup

That's it!

@lestephane
Copy link

lestephane commented Aug 11, 2019

Nice gist, thanks! I found it useful to log when a message is inspected, and when it is deleted, for later troubleshooting:

function cleanUpAfterOneWeek() {
  var delayDays = 7 // Enter # of days before messages are moved to trash
 
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays);
   
  var label = GmailApp.getUserLabelByName("cleanup-after/1week");
  var threads = label.getThreads();
  for (var i = 0; i < threads.length; i++) {
    console.log("inspecting message:%s date:%s",  // <<<<
      threads[i].getFirstMessageSubject(),
      threads[i].getLastMessageDate());
    if (threads[i].getLastMessageDate()<maxDate)
      {
        threads[i].moveToTrash();
        console.log("deleted message"); // <<<<
      }
  }
}

@dariozandolinsgs
Copy link

dariozandolinsgs commented Jan 8, 2023

I suggest to consider also category. This my code:

function auto_delete_email(){
  //delete_Label ("Travel",365);
  delete_Category ("Social",7);
  delete_Category ("Forums",7);
  delete_Category ("Promotions",7);
}

function delete_Label(mailLabel,delayDays) {  
  var label = GmailApp.getUserLabelByName(mailLabel);   
  if (!label) {return false;}
  var maxDate = new Date(); 
  maxDate.setDate(maxDate.getDate()-delayDays);    
  var threads = label.getThreads();  
  for (var i = 0; i < threads.length; i++) {  
    if (threads[i].getLastMessageDate()<maxDate){  
      threads[i].moveToTrash();
    } 
  } 
}

function delete_Category(mailCategory,delayDays) {  
  var threads = GmailApp.search('category:' + mailCategory + ' ,older_than:'+ delayDays +'d');
  for (var i = 0; i < threads.length; i++) {  
      threads[i].moveToTrash();
  } 
  //500 means that reached the limit of search and there are additional items - so recures till this is less than 500
  if (threads.length == 500) delete_Category(mailCategory,delayDays);
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment