Skip to content

Instantly share code, notes, and snippets.

@GabeBenjamin
Created May 6, 2017 01:00
Show Gist options
  • Save GabeBenjamin/3ef20889fa37ae97e9492e58e90db892 to your computer and use it in GitHub Desktop.
Save GabeBenjamin/3ef20889fa37ae97e9492e58e90db892 to your computer and use it in GitHub Desktop.
Google script to automatically archive emails after x number of days.
// Original author fwed (contact@fwed.fr)
// Modified from
// https://gist.github.com/anonymous/2cca33d376f7f924fdaa67891ad098cc
// https://medium.com/@fw3d/auto-archive-emails-in-gmail-after-2-days-1ebf0e076b1c
function gmailAutoArchive() {
gmailAutoarchiveHelper(1);
gmailAutoarchiveHelper(2);
gmailAutoarchiveHelper(3);
gmailAutoarchiveHelper(7);
}
function gmailAutoarchiveHelper(numDays) {
Logger.log('Running archiver for numDays: %s', numDays);
var delayDays = numDays; // will only impact emails more than numDays days
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
// Get all the threads labelled 'autoarchive'
var label = GmailApp.getUserLabelByName("autoarchive-" + numDays);
if (label == null || label == undefined) return -1;
Logger.log('Found label: %s', label.getName());
var threads = label.getThreads(0, 500).filter(function(thread) {
// Only include threads older than the limit we set in delayDays
return (thread.getLastMessageDate() < maxDate && thread.isInInbox());
});
Logger.log('Found %s emails.', threads.length);
var batch_size = 100;
while (threads.length) {
var this_batch_size = Math.min(threads.length, batch_size);
var this_batch = threads.splice(0, this_batch_size);
GmailApp.markThreadsRead(this_batch);
GmailApp.moveThreadsToArchive(this_batch);
}
}
@BugBandit
Copy link

This is really great, Gabe. Am I correct that these variables
gmailAutoarchiveHelper(1);
gmailAutoarchiveHelper(2);
gmailAutoarchiveHelper(3);
gmailAutoarchiveHelper(7);

are where I would enter nameOfLabel(#days) ?

Thanks.

@mmj3
Copy link

mmj3 commented Oct 23, 2017

@BugBandit

It looks like the script doesn't need any editing (unless you want to change the label it's looking for or the number of days it waits to archive.)

But here's how to use it without editing the script:
Pretty much just follow the instructions here:
https://medium.com/@fw3d/auto-archive-emails-in-gmail-after-2-days-1ebf0e076b1c

That is: setup gmail to automatically label all or certain incoming messages. Except with this script you'll need to use one of these labels: autoarchive-1 or autoarchive-2 or autoarchive-3 or autoarchive-7. And pick one of those labels based on how many days you want it to wait to archive your message.

And of course:
Setup a daily trigger
Within your Google Script window:
Click on Edit > Current Project's Triggers
Then add a new trigger with the following settings: Time-driven, Hour timer, Every 12 hours (or something similar)

I set mine up to run once daily. I changed the label the script was looking for to a-(number) instead of autoarchive-(number)

I used this line:
var label = GmailApp.getUserLabelByName("a-" + numDays)
instead of this line:
var label = GmailApp.getUserLabelByName("autoarchive-" + numDays)

Because I don't like having long labels showing on all of my gmail emails. (After changing that line it then looks for messages labeled with a-1 or a-2 or a-3 or a-7)

And I remarked this line:
// GmailApp.markThreadsRead(this_batch);
Because I don't want it marking my archived messages as read. They might not have been read, I might want to go back and see which archived messages I didn't read! Although I might never look at them again (hah), if I do, I'd still like to know whether they were actually read or not.

@gstrout
Copy link

gstrout commented Jun 6, 2018

I'm trying to find a way to target emails which have only one label. I want to archive messages which have a specific label and are older than 2 weeks. However, I want the script to ignore any emails which have additional labels besides the specified label.

For example an email with label 'autoarchive' gets archived, but an email with label 'autoarchive' AND 'Keep!' will not.

It looks like the current version above will archive any email with the specified label regardless of any other labels applied.

Does anyone have any ideas on how to accomplish this?

@tedsteinmann
Copy link

tedsteinmann commented Aug 27, 2018

Thanks for the updates.

Depending on your workflow, I'd question the marking as read. I just removed the following:

GmailApp.markThreadsRead(this_batch);

I like to file and move according to an automated process, but would rather not mark as read if I haven't actually read the email.

@tedsteinmann
Copy link

I forked from a similar repository and made a few updates from your gist. Specifically, I added support for delete along with archive and changed the naming convention to support nesting. (e.g. 'auto/archive/daily' and 'auto/delete/monthly')

https://gist.github.com/tedsteinmann/cefa922151b04c6a0957c9ee3091534e

Thank you,

@karpada
Copy link

karpada commented Jan 1, 2020

I've added inferring the interval from the label.
e.g. labal autoarchive3days implies archive after 3 days
https://gist.github.com/karpada/43910361c60051e077bbe76e7e6619bb

@LPapapo
Copy link

LPapapo commented Mar 10, 2022

Why don't you use the filter to create a label:inbox older_than:1d ? Emails older than one day should be archived. What is the difference between this method ?

@relativenoob
Copy link

Why don't you use the filter to create a label:inbox older_than:1d ? Emails older than one day should be archived. What is the difference between this method ?

Maybe because the method you mention doesn't actually work (filters only perform actions when the email is received into your gMail account - filters do nothing for emails already in your mailbox, so using a filter to move emails older than a certain date doesn't work (despite the "guides" out there professing otherwise).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment