Time Based Gmail Filters
I filter everything in gmail. Everything is automated for me, including reminder emails. Recently, I wanted to automate the deletion of these reminder emails after a certain time frame.
After some Googling, I found this stellar post by John Day. It runs you through using Google Scripts and the Gmail API to accomplish just what I wanted.
I wanted to delete my reminder emails after two days automagically. This script did just that:
Javascript
function cleanUp() {
var delayDays = 2;
var maxDate = new Date();
var label = GmailApp.getUserLabelByName("Delete-me");
var threads = label.getThreads();
maxDate.setDate(maxDate.getDate() - delayDays);
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate() < maxDate) {
threads[i].moveToTrash();
}
}
}
This is looping over the email threads you have in your Gmail with the label Delete-me, then adding them to the trash. You can see the list of available functions you can call on a thread in the Gmail API docs.
After running the script to test it, you can set it up to run on an interval under Resources > Current project’s triggers. I set mine up to run every 30 minutes. Now everything I setup to tag with Delete-me will be deleted automatically if it’s older than two days.
Leave a Reply