Skip to content

Instantly share code, notes, and snippets.

@bryceml
Created March 14, 2016 02:57
Show Gist options
  • Save bryceml/148e8deed7091963b02f to your computer and use it in GitHub Desktop.
Save bryceml/148e8deed7091963b02f to your computer and use it in GitHub Desktop.
Auto Delete, Archive, and Fowrard Gmail with Google Scripts
//To install go to https://www.google.com/script/start/ and setup the triggers
function cleanUp() {
var delayDays = 7 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("delete me after 7 days");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToTrash();
}
}
}
function archiveInbox() {
// Every thread in your Inbox that is read, older than two days, and not labeled "delete me".
var threads = GmailApp.search('label:inbox is:read older_than:2d -label:"delete me"');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
//Forwards everything with the "forward me please" label to an email address, and then removes the label
function forward() {
var label = GmailApp.getUserLabelByName("forward me please");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++){
messages[j].forward("userToSendTo@example.com");
}
threads[i].removeLabel(label);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment