Last active
October 20, 2020 12:27
-
-
Save vinloo/a5d772fd8d396c3e1e5cda199a28d54f to your computer and use it in GitHub Desktop.
Google App Script for Gmail Auto Archiving
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
| /* Paste this code in scrip.google.com. | |
| * You have to manually add a (daily) trigger: | |
| * Edit -> Current project's triggers -> Add trigger */ | |
| function RunAutoArchive(){ | |
| var threadCount = 0; | |
| // All read emails older than 1 month will be archived | |
| var searchQuery = 'label:read in:Inbox older_than:1m'; | |
| var threads = GmailApp.search(searchQuery); | |
| threadCount = threads.length; | |
| if(threadCount > 0){ | |
| // Split the array in chunks of 100 because of Google's limit | |
| var i, threadChunk, chunk = 100; | |
| for (i=0; i<threadCount; i+=chunk) { | |
| threadChunk = threads.slice(i,i+chunk); | |
| GmailApp.moveThreadsToArchive(threadChunk); // Do the archiving | |
| GmailApp.refreshThreads(threadChunk); | |
| } | |
| } | |
| return threadCount; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment