Last active
July 8, 2021 20:28
-
-
Save benwerd/209cad037d1067baff919ce98be98c05 to your computer and use it in GitHub Desktop.
Automatically archive unimportant emails after a week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I run this on a timer from scripts.google.com. You'll need to manually approve permissions the first time you run it. | |
function gmailAutoArchive() { | |
let delayDays = 7; // I use seven days; you can change the archival threshold in days by changing this number. | |
let search = `in:inbox {-in:important category:social category:updates category:promotions category:forums} -in:starred older_than:${delayDays}d`; | |
let offset = 0; | |
let threadCount = 1; | |
let limit = 100; // GmailApp bulk methods can only handle 100 threads at a time | |
while(threadCount > 0) { | |
let threads = GmailApp.search(search, offset, limit); | |
threadCount = threads.length; | |
console.log('Found ' + threadCount + ' threads with offset ' + offset); | |
console.log('Marking as read ...'); | |
GmailApp.markThreadsRead(threads); | |
console.log('Archiving ...'); | |
GmailApp.moveThreadsToArchive(threads); | |
offset += threadCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment