How to set GMail to Auto-Delete emails after scheduled time?

Updated on September 1, 2017

Can you make your GMail inbox with zero emails? Probably, that’s not possible (at least for some people). But you can clean up old emails after a set number of days. A bit of script in Google Apps can automatically archive or delete old emails based on the schedule you had set and keeps your inbox a lot more cleaner. For example, most of you might receive promotional and offer coupons from various service providers; which can be auto deleted after their expiry dates (probably after two days). This is possible using Google scripts.

All you need to do is, try to filter the emails that are mostly useless to you. To do that, you can create filters in GMail, by clicking the gear wheel on the top-right corner and select settings, Filters tab and click the ‘Create a new filter” link at the bottom. Here, you will find options to create a filter. Label the filter as ‘delete me’ and apply it to all the existing messages.

Step 1: Go to Google Scripts and create a new blank project. You will have to copy and paste the below script in the new file and run it from the Run menu.

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();
}
}
}

If you want to change, when the emails with ‘delete me’  should be sent to Trash, then you will have to change the value of ‘delayDays’ in the above script.

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

Script 2: Once the script is created, you will have to set a trigger for it. I mean, you need to tell when the script should run in a day. To do that, click Resources > Current project’s triggers and make it to run on a pre-minute, hourly or daily basis.

Google scripts for automatically cleanup old emails

Step 3: You are done. Run the script to move the emails labeled as ‘delete me’ to Trash older.

ALSO READ: How to Undo the sent mail in GMail?

Was this article helpful?

Related Articles

Leave a Comment