Skip to content

Instantly share code, notes, and snippets.

@valmayaki
Forked from SciutoAlex/googleScript.js
Created March 1, 2024 18:10
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 valmayaki/e8144339ceb460e6b6b12814f1500b81 to your computer and use it in GitHub Desktop.
Save valmayaki/e8144339ceb460e6b6b12814f1500b81 to your computer and use it in GitHub Desktop.
Archive Gmail Regularly
// Code modified from: http://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script
// This is code for a Google Apps Script. You can add the code and give it permissions at script.google.com
// Archive every thread in your Inbox that is older than two days, and not starred.
function archiveInbox() {
var threads = GmailApp.search('label:inbox older_than:2d -in:starred');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
// Archive every thread in your tabs that is older than one day, and not starred.
// valid categories include ["primary", "promotions", "social", "updates", "forums"];
function batchArchiveCategories() {
var categories = ["social", "promotions", "updates"];
var batchSize = 100 // Process up to 100 threads at once
for (i = 0; i < categories.length; i++) {
var threads = GmailApp.search('label:inbox category:'+categories[i]+' older_than:1d -in:starred');
for (j = 0; j < threads.length; j+=batchSize) {
GmailApp.moveThreadsToArchive(threads.slice(j, j+batchSize));
}
}
}
// To run regularly, click Resources >> Current Project's Triggers.
// Choose runAll() and set how often you want the script to execute.
function runAll() {
batchArchiveCategories();
archiveInbox();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment