Delete Old Gmail Emails Automatically With this Google Script

There is not doubt that Gmail is one of the most used email service all over the world. There are so many reasons which make Gmail one of the most popular email service. With the help of Gmail Lab Addons you can do lots of thing on your inbox and lots of other feature which you will enjoy on your inbox. Check out some of the best Gmail tools and how to guides here. Today, in this tutorial we will show you how you can automatically delete emails after X number of days.

In our previous articles, we’ve discussed about lots of gmail tips like How to Clean up Gmail and Get Space back, Recover Deleted Emails From Gmail Inbox and how to Remove Spam Mails Automatically From Gmail. Today, we found a new Google Script which allow you to delete your old email automatically and help you to get rid of expired daily deals emails.

All you have to install the Google Script, which you’ll need to authorize with your Gmail account. After that set the Label for your unwanted emails and the script will delete all those labeled emails automatically. Lets take a look how this Google Script work with your Gmail account.



Delete Gmail Emails Automatically

1. Visit this Google Script Link to setup the script on your Gmail account. From here click on the Blank Project option to get started. See the screenshot below:-

Google Script

2.Now copy the following script and paste it on your Google Script window, save it.

function auto_delete_mails() {  
  var label = GmailApp.getUserLabelByName("Delete Me");  
  if(label == null){
    GmailApp.createLabel('Delete Me');
    var delayDays = 2 // Enter # of days before messages are moved to trash   
    var maxDate = new Date(); 
    var threads = label.getThreads();  
    for (var i = 0; i < threads.length; i++) {  
      if (threads[i].getLastMessageDate()<maxDate){  

3. Once the script is pasted on the scripts window, click on the “Current Project trigger” icon from the toolbar and set a trigger. (Current Project’s Triggers -> Add one now). One the trigger is set hit the Save button.


4. Once the trigger is setup on your account, go to your Gmail inbox account and you will see the new “Delete Me” Label on your account.

5. now all you have to do is tag all the unwanted email that you don’t want on your inbox with this “Delete Me” Label. And all those email will be deleted after 2 days.

Delete Me Label

If you want to change the expiry day from the script, then change delayDays number from the script. (var delayDays = 2 // Enter # of days before messages are moved to trash )

Looking for more Gmail tips and Guides, do check out our Gmail section here

Rajesh Rajput is Tech Blogger, who loves to write about Android Tips, How to Guides, Reviews, ROMs, and more.