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.

Related :  How to Fix “Imap Gmail Com :993:1” error in Windows 10 or 8.1 phone?

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“.


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.


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(); 
var label = GmailApp.getUserLabelByName("delete me");  
var threads = label.getThreads();  
for (var i = 0; i < threads.length; i++) {  
  if (threads[i].getLastMessageDate()<maxDate)

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

Related :  Google One Android - How to Add Family Member using Mobile Number or Email

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



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.




Next you need to grant privileges to your Google account.





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…




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.




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.




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.

4 thoughts on “How to Auto-Delete Email After Time in Gmail using Google Script?”

  1. any help on how it would expand this script to only delete emails larger than XX kb and older then xx days. I also don’t want it to delete the whole thread just the messages that exceed the kb threshold.

  2. This was ingenious… I’ve been searching for something to “auto-delete” certain emails after a delay that I pick for years! This will definitely help me control my inbox.

    Question: how can I modify the script to “Archive” certain emails after a time delay? I’d like emails from certain companies to go to certain folders/labels that I choose.

    Thanks for this!

  3. Hola! I’ve been following your blog for a while now
    and fionally got the bravery to go ahead and give
    you a shout out from Austin Tx! Just wantsd to say keep up the fantastic work!


Leave a Comment