How to Auto-Delete Email After Time in Gmail using Google Script?

Gmail is an all-in-one email service with many options and features. The filter function is useful if you want to prevent clutter of emails filling your inbox. Normally this is resourceful to label certain messages from a sender or to a receipient. But today we are going to use the same concept to auto-delete emails after time in Gmail.

How To Create a Google Script to Auto-Delete Email after Time in Gmail?

There are 3 basic steps.

  • Create a filter in Gmail.
  • Create a Google Script.
  • Create a Trigger.

How to Create a Filter in Gmail named “delete me”?

First you need to create a filter based on a condition for the messages you want to auto-delete. This process is very simple.

When you click the gear-icon, there appears a drop-down menu. The gear-icon is visible just above your email or inbox. Select “Compact“. Then the screenshot looks something like below. I have customized my Gmail interface with theme and some other options. So if you don’t find it the same way don’t get bamboozled.

Next at the top there is the “More” drop-down.

The easy way to create a filter is to select the email messages for which you want to create a filter. This will automatically fill the “From” text-box in the condition pop-up menu that opens.

Filter Messages in Gmail

 

When you click the “Filter Messages like these” then a pop-up rises up with the condition text-boxes.

Search Filter with Conditions in Gmail

 

You can set various filter conditions to gather the required email messages. There are different criterion like “From”,”To”,”Subject”,”Has the words”,”Doesn’t have”, “Has attachment” etc which can be utilized to group and filter your email messages.

After you are done with your search criteria, you need to click the link “Create filter with this search“. This will take to the next screen.

Here you need to create a new label for your filtered messages. Name the label as “delete me“. This can be done by checking the check-box Apply the label and New label…. You can also select the previous conversations or messages by checking the check-box that says “Also apply filter to <no.> matching conversations“.

apply_label_in_gmail

With this, the filtering process is over.

How to Create a Google Script with the same Google Account ?

Next you need to create a Google Script.

The first time you create a script, an introduction screen is displayed. After this you are provided with developmental screens.

blank_project_google_script

You need to create a “Blank Project“. Then in the text-box or tab that appears you need to paste the following code.

function cleanUp() {  
var delayDays = 2 // Enter # of days before messages are moved to trash   
var maxDate = new Date(); 
maxDate.setDate(maxDate.getDate()-delayDays);    
var label = GmailApp.getUserLabelByName("delete me");  
var threads = label.getThreads();  
for (var i = 0; i < threads.length; i++) {  
  if (threads[i].getLastMessageDate()<maxDate)
  {  
    threads[i].moveToTrash();
  } 
} 
}

Generally it’s saved under the file name Code.gs. When you click the save button, it asks for a new project name, which you need to provide.
google_script_code_auto_delete_emails_after_time_gmail

After the project name is provided, the interface will look something like below. From the menu, you need to Run >cleanUp.

run_project_google_script

 

When you run the above google script, you are asked for authorisation to run the above script using the same Google account in which you create the filter for your Gmail.

 

authorisation_for_running_google_script

 

Next you need to grant privileges to your Google account.

 

grant_access_run_google_script

 

 

This completes the authority and grant access permissions. The Google Script is created and you are allowed to run the script.

How to Create a Trigger for your Google Script ?

The last stage is to create a Trigger for this script. To do this, from the menu you need to select Resources > Current project’s triggers…

 

triggers_resources_google_script

 

The first time, the trigger is created, it gives a note message and asks you to create one. Once you click the link you can create a trigger.

 

new_trigger_google_script

 

Here is the final stage where you will tell the script, when to run. Though you have set in the code, to auto-delete email messages to trash for every 2 days, you need to set a time period for the trigger to run. There are several options like  Minutes timer, Hour timer, Day timer etc. using which you can set the time in minutes, hours, days etc for the script to run. Click on Save when the process is complete.

 

current_project_s_triggers

 

That’s about it. When you run the script now, you can see that all emails or conversations with “delete me” label have been moved to Trash. There is a minute tweak or setting which you can change.

var delayDays = 2 // Enter # of days before messages are moved to trash

By changing the number “2” in the above code line to the desired number, you can tell Gmail how long to keep the email in your inbox before the Google Script sends it to Trash.

One Response

  1. Kory May 13, 2015

Leave a Reply