Gmail Scripts (Dynamically Manage Your Inbox)

This is a new venture for me.  Although this won’t be a very in depth of all the possibilities of Gmail scripting, it will give a quick example of what’s possible.

A major drawback that Gmail had (past tense) in my mind was that there was no cleanup.  So, contrasted with outlook.com, it appeared to be missing an important feature.  There was no way to clear out old messages except for manually finding them and pressing delete.

Now, that is changed.  Below I will give a simple script that can clear out your Gmail mess based on search criteria.

Go to your Google Drive and press “Create” -> “Script”.  Click “gmail” (under “create script for”).  You’ll see a template that Google has created for you.  You can clear that out, if you’d like.

Add this code:

[code] function kickOff(){
deleteOldUnwantedMessages(“Newsletters”,”3d”);
deleteOldUnwantedMessages(“Newsletters-Political”,”15d”);
deleteOldUnwantedMessages(“Newsletters-SQL-Server-Central”,”30d”);
deleteOldUnwantedMessages(“facebook”,”3d”);
deleteOldUnwantedMessages(“BULK”,”3d”);
deleteOldUnwantedMessages(“trash”,”30d”);
autoArchiveOldInboxItems();
}

function deleteOldUnwantedMessages(label_to_search,older_than){

var search_term = “”;
/*Build string to search*/
if(label_to_search == “trash”){
search_term = “!label:starred in:” + label_to_search + ” older_than:” + older_than;
}else{
search_term = “!label:starred label:” + label_to_search + ” older_than:” + older_than;
}

/* Limit 100 per delete. Get groups up to 100 and loop through delete process for each group. */
while( GmailApp.search(search_term).length > 0){
var threads = GmailApp.search(“label:” + label_to_search + ” older_than:” + older_than,0,100);
GmailApp.markThreadsRead(threads);
GmailApp.moveThreadsToTrash(threads);
}
}

function autoArchiveOldInboxItems() {
var search_term = “!label:starred label:inbox older_than:30d”;

while(GmailApp.search(search_term).length > 0){
var threads = GmailApp.search(search_term,0,100);

GmailApp.markThreadsRead(threads);
GmailApp.moveThreadsToArchive(threads);
}

}
[/code]

Notice that I used Newsletters, facebook, BULK, and trash in my arguments.  You can replace that with whatever labels you want to search for.  Also, update the second argument (3d, 30d, etc.).  This is the amount of time.  3d is 3 days, 30d is 30 days.  Make it suit your needs.  Add more lines or remove unwanted lines to suit your needs.

[code] /* example */
deleteOldUnwantedMessages(label_name,time);
[/code]

Press save.

Go to File, Manage Versions, and press save to make a “version” out of it.

Go to Publish, Deploy as Webapp.  You’ll have to give it permissions to run, etc.

Go to Resources, All your triggers, and choose how often you’d like it to run.

Done!

 

Take note: This does not execute quickly.  Give it time.  It’s not the usual 2 second turn-around time we’re used to with Google.

This site uses Akismet to reduce spam. Learn how your comment data is processed.