Skip to content

Instantly share code, notes, and snippets.

@mikemoate
Created February 26, 2019 11:28
Show Gist options
  • Save mikemoate/bacde444ccac261c6fe7ef42be1b3160 to your computer and use it in GitHub Desktop.
Save mikemoate/bacde444ccac261c6fe7ef42be1b3160 to your computer and use it in GitHub Desktop.
// Google Apps Script for use at https://script.google.com/
// Use timed triggers to run the desired functions on a regular schedule (e.g. every 12 hours)
// search is a gmail search string, keepDays is the last N days of threads to keep in Inbox
function gmailAutoArchive(search, keepDays) {
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-keepDays);
var threads = GmailApp.search(search);
// archive all the threads where the last message is before maxDate
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].markRead();
threads[i].moveToArchive();
Logger.log("Archived: %s", threads[i].getFirstMessageSubject());
}
}
}
function gmailAutoArchiveRead() {
// limit archiving to read threads, where the last message is more than 14 days ago
gmailAutoArchive('in:inbox is:read', 14);
}
function gmailAutoArchiveNotifications() {
// limit archiving to threads with the notifications label, where the last message is more than 7 days ago
gmailAutoArchive('label:notifications', 7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment