Skip to content

Instantly share code, notes, and snippets.

@wrboyce
Created May 12, 2015 17:16
Show Gist options
  • Save wrboyce/4a1ffee94d8dcb75b986 to your computer and use it in GitHub Desktop.
Save wrboyce/4a1ffee94d8dcb75b986 to your computer and use it in GitHub Desktop.
function autoArchive() {
var archiveLabels = ["<...>"],
deleteLabels = ["<...>"],
maxAgeDays = 7,
log = DocumentApp.openById("<Log Document ID>"),
logger = log.getBody();
var maxDate = new Date();
logger.appendParagraph(maxDate.toString());
maxDate.setDate(maxDate.getDate()-maxAgeDays);
for (var i = 0; i < archiveLabels.length; i++) {
var label = archiveLabels[i];
var threads = GmailApp.search("is:read in:inbox label:" + label);
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
if (thread.getLastMessageDate() <= maxDate) {
logger.appendParagraph("Archiving Thread: " + thread.getFirstMessageSubject());
GmailApp.moveThreadToArchive(thread);
}
}
}
for (var i = 0; i < deleteLabels.length; i++) {
var label = deleteLabels[i];
var threads = GmailApp.search("is:read label:" + label);
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
if (thread.getLastMessageDate() <= maxDate) {
logger.appendParagraph("Deleting Thread: " + thread.getFirstMessageSubject());
thread.addLabel("fake-bin");
GmailApp.moveThreadToArchive(thread);
}
}
}
logger.appendHorizontalRule();
log.saveAndClose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment