Thursday, November 22, 2012

How to configure Gmail to automatically delete some emails after certain number of days?

This article will show you how to configure your Gmail to automatically delete some emails after certain number of days or hours. For example, you may want to keep some email with "daily promotions" only for 24 hours, or you want to keep Amazon newsletters only for few days.

Unfortunately, Gmail built-in filters could not be used for that task because they only work on incoming emails. That is, you cannot automatically process emails already in your inbox.

Described approach uses combination of filter and Google Apps Script - filter applies predefined label to all emails you want to keep only for few days and script is invoked daily, it scans all emails marked with the label and deletes these old enough.

Here's how to install and configure the script:

Open any spreadsheet from you Google drive tab, or create a new empty one

1) Click on a Tools top menu and select Script gallery...

2) Put "newsletters" in a search filed and so search
3) You will find my script named "Remove newsletters older than 24 hours"

4) Install it for your Google account, and after that you will be able to access it from Tools / Script manager menu and open for editing

Now that you have installed the script you need to run it once a day to actually clean up your Inbox and for that you need to set up a trigger:

1) Opened the scrip in editor:

2) Select Resources / Current script's triggers from menu.

3) Create a new trigger and configure it as Time driven, Day timer, Midnight to 1am - that will make this script execute daily at midnight on your Gmail account.

Ok, that is almost it... one more small step.

What this script is doing, it scans your inbox looking for emails with specific label - "Newsletters".
All emails with this label older than 24 hours are than moved to trash.

So for this to work you actually need to have this label applied to all your newsletter emails, but that is something easily done with standard Gmail filters - so I will not cover it here. 


  1. Can the time be customized? I.e., can it be changed from 24 to 72 hours, or some other interval?

    1. Yes, script is very simple - you can adjust it to use any interval.
      Right near the top, you will see:

      var h24 = 1000*60*60*24;

      This is maximum time for emails to live in your inbox, in milliseconds.
      You just need to change it and thats it.

  2. Very nice script, but when run, I get errors from this line of the code:
    var threads = label.getThreads();

    Error reads:
    TypeError: Cannot call method "getThreads" of null. (line 5, file "DelfourEDI")

    What am I doing wrong?

    1. It seems that you don't have right label created, that is why label is NULL and you see error. How that scrip works - it gets the label, than gets all the emails from that label.
      So just make sure you have label named same way in script and in your Gmail.

  3. Hello, could you please fix the pictures, the image hosting service seems to be down. Thank.

    1. Evernote... I will fix it over weekend.

    2. still not fixed, but tutorial still easy to follow. thanks

  4. still we are not able to view the screen shots/ pictures

  5. Pl confirm will this scripts work if there are more then 1000 mail in the labeled box.

  6. yes, I have over 5000 emails in my box and it works

  7. Thank you for the helpful script. It wasn't working, and I noticed an apparent comparison issue:
    threads[i].getLastMessageDate() returns a date object, and `deadline` is an int, so the comparison never returns true and never moves the thread to the trash. I added Date.parse() around threads[i].getLastMessageDate() to fix it.

    if( Date.parse(threads[i].getLastMessageDate()) < deadline ){...}

    Hope this helps somebody.

  8. any way to change this script to purge older items in SENT MAIL, not in the INBOX?

  9. Yes, I would like this for the sent box too.