A Formula For Deleting Promo Emails by Breanne

In MWW42, Habbi and Hailley discussed Habbi’s simple automation for keeping track of bookings and receipts. The episode sparked something with many listeners, as keeping on top of email can be both tedious and daunting. Long-time listener Breanne reached out after listening to share that she had her own formula she created to auto-delete promotional and sales emails because unsubscribing had become too time-consuming.

Source

Breanne was kind enough to elaborate both on how she set it up and share instructions for anyone looking to do the same. Here’s Breanne:

So initially I had a filter set up within Gmail that deleted any emails within the promotions category after 30 days. However, that stopped working at one point (I'm not sure when or why). Anyway, I revamped the system and now have a script running from Google Drive - but it needs the email addresses of the promotional emails coming in. So when you go to promotions, if you click the box in the top left, it'll select all the emails, then click the three dots on the right and select "filter messages like these" - that'll populate the search field with "from:(promotional email addresses)" - you'll need this for the script.

Then in Google Drive, I clicked New Google Apps Script and started a new script. Under Services, I had to add Gmail. Then here's my Script, it deletes anything older than 7 days, giving me a bit of time to actually capitalize on any sale I might care about:

function ProcessOldMails () {
//actions: moveToTrash
var rules = [
{query: "from:(this is where you'll put the email addresses)", days: 7, action: "moveToTrash"},
];
for(var j=0;j<rules.length;j++){
var emails = GmailApp.search(rules[j].query);
for(var i=0;i<emails.length;i++){
if(((new Date() - emails[i].getLastMessageDate())/1000/24/3600) > rules[j].days){
switch(rules[j].action){
case 'moveToTrash':
emails[i].moveToTrash();
break;

}
}
}
}
}

Then under Triggers I set a trigger to run the script on a time-based value. I have mine set to run daily.

I hope it helps!!!”

Here are the step-by-step instructions on auto-deleting promotional and sales emails in Gmail:

  • Navigate to the Promotions tab in Gmail
  • Click on the box in the top left to select all emails
  • Click the three dots on the right and select “filter messages like these” (This will populate the search field with "from:(promotional email addresses)" and you'll need that information for the script.)
  • Nagivate to Google Drive
  • Click “New” from the top left, then “more” to see an option for Google Apps Scripts
  • From Google Apps Scripts, click the + next to Services and add Gmail
  • Add the following script
function ProcessOldMails () {
//actions: moveToTrash
var rules = [
{query: "from:(this is where you'll put the email addresses)", days: 7, action: "moveToTrash"},
];
for(var j=0;j<rules.length;j++){
var emails = GmailApp.search(rules[j].query);
for(var i=0;i<emails.length;i++){
if(((new Date() - emails[i].getLastMessageDate())/1000/24/3600) > rules[j].days){
switch(rules[j].action){
case 'moveToTrash':
emails[i].moveToTrash();
break;

}
}
}
}
}
  • Navigate to Triggers
  • Set a trigger to run the script on a time-based value (Breanne runs hers daily)

Thank you so much to Breanne for sharing this graceful solution with us all! Hopefully your future holds an inbox filled with fewer promotional emails. ✨