Skip to content

Instantly share code, notes, and snippets.

@vinloo
Last active October 20, 2020 12:27
Show Gist options
  • Select an option

  • Save vinloo/a5d772fd8d396c3e1e5cda199a28d54f to your computer and use it in GitHub Desktop.

Select an option

Save vinloo/a5d772fd8d396c3e1e5cda199a28d54f to your computer and use it in GitHub Desktop.
Google App Script for Gmail Auto Archiving
/* 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