Skip to content

Instantly share code, notes, and snippets.

@davidbauer
Created September 30, 2018 14:07
Show Gist options
  • Save davidbauer/586cb366110e15f7c29cb111f76f2951 to your computer and use it in GitHub Desktop.
Save davidbauer/586cb366110e15f7c29cb111f76f2951 to your computer and use it in GitHub Desktop.
Automatically archive emails in Gmail after x days
// how to use this
// 1. create a new script on https://script.google.com/home and paste over this code
// 2. define which emails you want to archive and after which delay (lines 7,8)
// 3. add a «project trigger» to define how often this script should be executed (e.g. every day at midnight)
function auto_archive_mails() {
var label = GmailApp.getUserLabelByName("News Briefings"); // Emails with which label do you want auto-archived?
var delayDays = 3 // After how many days do you want your emails auto-archived?
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var threads = label.getThreads();
if (threads.length > 0) {
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate){
threads[i].markRead();
threads[i].moveToArchive();
}
}
}
}
// adapted from https://www.maketecheasier.com/google-scripts-to-automate-gmail/amp/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment