-
-
Save anonymous/2cca33d376f7f924fdaa67891ad098cc to your computer and use it in GitHub Desktop.
function gmailAutoarchive() { | |
var delayDays = 2; // will only impact emails more than 48h old | |
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"); | |
var threads = label.getThreads(0, 400); | |
// we archive all the threads if they're unread AND older than the limit we set in delayDays | |
for (var i = 0; i < threads.length; i++) { | |
if (threads[i].getLastMessageDate()<maxDate) | |
{ | |
threads[i].moveToArchive(); | |
} | |
} | |
} |
Hi all, after playing around with this I came up with a more efficient version which uses search to find all affected threads instead of iterating through threads belonging to the label. Hope it helps someone.
Replace moveThreadsToArchive with moveThreadsToTrash if you want to trash the threads instead.
function gmailAutoarchive() { var rules = { // Each label with number of days to keep "billsbillsbills" : 2, "newslettersandstuff" : 28 } var batch_size = 100; for (let label in rules) { var keepdays = rules[label]; Logger.log(`Archiving all threads in ${label} older than ${keepdays} Days`); var threads = GmailApp.search(`label:inbox label:${label} older-than:${keepdays}d`); Logger.log(`Identified ${threads.length.toString()} threads for archival in ${label}`); 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 know this is very old, but if anyone else finds this you're actually reprocessing all of the archived emails every time. Adding label:inbox
limits the search to the emails not already archived. I've modified the above to do that, and also to mark the messages as read while moving them.
Thank you for the solutions everyone. I had to change older-than to older_than in @blhoward2's solution and it worked perfectly.
function gmailAutoarchive() {
var rules = { // Each label with number of days to keep
"autoarchive" : 2,
"newslettersandstuff" : 28
}
var batch_size = 100;
for (let label in rules) {
var keepdays = rules[label];
Logger.log(`Archiving all threads in ${label} older than ${keepdays} Days`);
var threads = GmailApp.search(`label:inbox label:${label} older_than:${keepdays}d`);
Logger.log(`Identified ${threads.length.toString()} threads for archival in ${label}`);
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);
}
}
}
Hey, i am looking to archive all my "read" mails which have "user labels" after 2 days, can anyone help with that?. i want to do it for all mails user labels instead of doing individually for all the labels i have. i also do not want to create a separate label to use as an archived inbox if possible. if not i will go this way
@101legend101 this is my approach, quite simple really. Cheers.
function gmailAutoarchive() {
// Build the query
var query = "in:inbox is:read has:userlabels older_than:2d";
// Get the threads
var threads = GmailApp.search(query);
// Archive the threads
for (var thread of threads) thread.moveToArchive();
}
Thanks for this, I tweaked it for my needs and it is available here Archives any read message that is older than 24 hours.
Hi, I further tweaked your script and feel I made some worthwhile changes. A bit more verbose in the logging, but mainly it is more efficient in memory management and loops if the files to archive are >500. You can find the my updated version here.
Thanks for the starting point. :)
do you know if this impact snoozed emails? (let's say an email that older than 2 days that has been snoozed to reappear in two weeks)