Skip to content

Instantly share code, notes, and snippets.

@owendl
Forked from anonymous/gmailAutoarchive.js
Last active March 30, 2021 00:31
Show Gist options
  • Save owendl/dbf9724b1ab82a39a93e546b27627845 to your computer and use it in GitHub Desktop.
Save owendl/dbf9724b1ab82a39a93e546b27627845 to your computer and use it in GitHub Desktop.
function gmailAutoarchive() {
var archived_threads = 0;
var delayDays = 2;
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
// getUserLabels returns an array of gmail labels.
// This way we don't have to convert the array elements and will archive all user labeled threads.
var labels = GmailApp.getUserLabels()
var threads = GmailApp.getInboxThreads(0, 400);
// 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++) {
var thread = threads[i];
if (thread.getLastMessageDate()<maxDate){
var thread_labels = thread.getLabels();
for (var j = 0; j < thread_labels.length; j++){
if (labels.includes(thread_labels[j])){
thread.moveToArchive();
archived_threads += 1;
break;
}
}
}
}
console.log("archived "+archived_threads+" threads")
}
@owendl
Copy link
Author

owendl commented Mar 21, 2021

This piece of code is designed to be run as a google script and auto archive threads to (try to) clean up my inbox. It will archive any thread in the inbox that a) is over 48 hours old and b) has a label (either auto or manually labeled).

Idea found from: https://medium.com/@fw3d/auto-archive-emails-in-gmail-after-2-days-1ebf0e076b1c, and forked from corresponding repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment