Skip to content

Instantly share code, notes, and snippets.

@WillsonSmith
Created October 29, 2018 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillsonSmith/82006eebc4bd68deb2fe5ac717308b47 to your computer and use it in GitHub Desktop.
Save WillsonSmith/82006eebc4bd68deb2fe5ac717308b47 to your computer and use it in GitHub Desktop.
function searchThreads(offset, max, threads) {
const searchedThreads = GmailApp.search("in: inbox older_than:365d", offset, max);
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-365);
return searchedThreads.filter(function(thread) {
return thread.getLastMessageDate() < maxDate;
});
}
function moveThreads(threads) {
var threadList = threads;
if (threadList.length > 100) {
GmailApp.moveThreadsToArchive(threadList.splice(0, 100));
moveThreads(threadList);
} else {
return GmailApp.moveThreadsToArchive(threadList);
}
}
function archive() {
var searchedThreads = searchThreads(0, 500, []);
moveThreads(searchedThreads);
const totalThreadsString = 'Archived a total of ' + searchedThreads.length + ' threads this week.';
GmailApp.sendEmail('email@example.com', 'Archived threads summary', totalThreadsString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment