Skip to content

Instantly share code, notes, and snippets.

@hannesbe
Created December 28, 2019 00:21
Show Gist options
  • Save hannesbe/525e5d9b87cdf9da9ea4fc6cc636e472 to your computer and use it in GitHub Desktop.
Save hannesbe/525e5d9b87cdf9da9ea4fc6cc636e472 to your computer and use it in GitHub Desktop.
Auto Archive Gmail script
// Original author fwed (contact@fwed.fr)
// Modified from
// https://gist.github.com/anonymous/2cca33d376f7f924fdaa67891ad098cc
// https://medium.com/@fw3d/auto-archive-emails-in-gmail-after-2-days-1ebf0e076b1c
function gmailAutoArchive() {
gmailAutoarchiveHelper(30);
}
function gmailAutoarchiveHelper(numDays) {
Logger.log('Running archiver for numDays: %s', numDays);
var delayDays = numDays; // will only impact emails more than numDays days
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
// Get all the threads labelled 'autoarchive'
var label = GmailApp.getUserLabelByName("M/autoarchive-" + numDays);
if (label == null || label == undefined) return -1;
Logger.log('Found label: %s', label.getName());
var threads = label.getThreads(0, 500).filter(function(thread) {
// Only include threads older than the limit we set in delayDays
return (thread.getLastMessageDate() < maxDate && thread.isInInbox());
});
Logger.log('Found %s emails.', threads.length);
var batch_size = 100;
while (threads.length) {
var this_batch_size = Math.min(threads.length, batch_size);
var this_batch = threads.splice(0, this_batch_size);
GmailApp.markThreadsRead(this_batch);
GmailApp.moveThreadsToArchive(this_batch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment