Skip to content

Instantly share code, notes, and snippets.

@superfawkes
Last active March 30, 2022 12:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save superfawkes/e998169702b4c521fb50e1afd20746ca to your computer and use it in GitHub Desktop.
Save superfawkes/e998169702b4c521fb50e1afd20746ca to your computer and use it in GitHub Desktop.
Gmail Inbox maintenance - automatically cleans up older email notifications that have only a transient use - either by sender or by filter-folder
// adapted from idea in https://gist.github.com/anonymous/2cca33d376f7f924fdaa67891ad098cc
function EmailIdFromMailer(mailer)
{
var start = mailer.indexOf('<');
var end = mailer.indexOf('>');
if (start > -1 && end > -1)
{
return mailer.substring(start+1,end);
}
return mailer
}
function gmailAutoCleanup()
{
var archiveDelayDays = 5; // will only impact emails more than 5d old
var archiveFromSender = ['alert@chess.com','googlealerts-noreply@google.com'] // add others as you see fit
var trashDelayDays = 30; // will only impact emails more than 30d old
var trashFolders = ['Social', 'online shopping']
var considerMsgWindow=60; //only messages in last 2 months will be processed.
var archiveThresholdDate = new Date();
archiveThresholdDate.setDate(archiveThresholdDate.getDate()-archiveDelayDays);
// Get first 400 emails from one-off notifiers and mostly-spammers
var threads = GmailApp.getInboxThreads(0,400);
if (threads == null || threads == undefined) return -1;
// we archive all the threads if they're unread AND older than the limit we set in delayDays
for (var i = 0; i < threads.length; i++)
{
messages = threads[i].getMessages();
if (messages == null || messages.length == 0)
{
Logger.log("*** Null messages...skipping");
continue;
}
if (threads[i].getLastMessageDate() < new Date() - considerMsgWindow) break;
var mailer=EmailIdFromMailer(messages[0].getFrom());
//Logger.log("considering '%s'...%s < %s", mailer, threads[i].getLastMessageDate(),archiveThresholdDate);
if (archiveFromSender.indexOf(mailer) > -1 && threads[i].getLastMessageDate() < archiveThresholdDate)
{
if (!threads[i].hasStarredMessages() && !threads[i].isImportant())
{
threads[i].markRead();
threads[i].moveToArchive();
Logger.log("Archiving thread %s : %s", messages[0].getFrom(), threads[i].getFirstMessageSubject());
}
else
{
Logger.log("Retaining (important/starred) thread %s : %s", messages[0].getFrom(), threads[i].getFirstMessageSubject());
}
}
else
{
//Logger.log(" Retaining thread %s : %s", messages[0].getFrom(), threads[i].getFirstMessageSubject());
}
}
var trashThresholdDate = new Date();
trashThresholdDate.setDate(trashThresholdDate.getDate()-trashDelayDays);
// Get emails in folders listed above
for (lbl in trashFolders)
{
var threads = GmailApp.getUserLabelByName(lbl);
if (threads == null || threads == undefined) continue;
for (var i = 0; i < threads.length; i++)
{
messages = threads[i].getMessages();
if (messages == null || messages.length == 0) continue;
if (threads[i].getLastMessageDate() < new Date() - considerMsgWindow) break;
if (threads[i].getLastMessageDate() < trashDelayDays && !threads[i].hasStarredMessages() && !threads[i].isImportant())
{
Logger.log("Trashing thread %s : %s", messages[0].getFrom(), threads[i].getFirstMessageSubject());
threads[i].moveToTrash();
}
else
{
Logger.log("Not trashing thread %s : %s", messages[0].getFrom(), threads[i].getFirstMessageSubject());
}
}
}
if (Logger.getLog().length > 0)
{
var email = Session.getActiveUser().getEmail(); # your email id
GmailApp.sendEmail(email, 'Auto-email Cleanup',
Logger.getLog() +
"Please restore trashed items within 30d.\nStar or mark important to prevent archival from Inbox or trashing from folder\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment