-
-
Save GabeBenjamin/3ef20889fa37ae97e9492e58e90db892 to your computer and use it in GitHub Desktop.
// Original author fwed ([email protected]) | |
// 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); | |
} | |
} |
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,
I've added inferring the interval from the label.
e.g. labal autoarchive3days
implies archive after 3 days
https://gist.github.com/karpada/43910361c60051e077bbe76e7e6619bb
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 ?
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).
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.