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, 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(){

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;
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( > 0){
var threads =“label:” + label_to_search + ” older_than:” + older_than,0,100);

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

while( > 0){
var threads =,0,100);



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 */

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.



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.