Gmail - Automatic Cleanup OF Emails From Specific Senders After A Specific Day

posted 19 Jan 2017, 08:36 by Dominique Cressatti   [ updated 29 Jun 2018, 11:16 ]

If like me you have subscribed to receive regular emails from a company, organization,  person, etc... (for example, I get notifications emails from Facebook, which I don't want to keep for more than a week) which you don't want/need to keep after a specific amount of days, the following Google Apps script (thanks to Sergei and Larry for giving us those great JavaScripting functionalities ;) ) will automatically do it for you.

1st go to, click on the Start scripting and past the following code.

  1. Manual search in Gmail for the sender's email address to make sure that the script will find the expected emails.
  2. Set the delayDays variable to a fairly high amount of days.
  3. Run the script manually before running it automatically with a trigger.

/* Quick Gmail script to delete emails from a specific sender 
   older than a specified amount of days
   For example, I get Facebook's notification in my inbox
   but I don't want/need to keep them for more than a week.

   Set the the delayDays and sender variables
   Tip: to make sure the script will find the emails you want,
   1st do a manual search in Gmail for the sender's email address.
   If it returns the expected emails, then you're good to go.

   To automatically run it, go into the "Resources" menu -> "All you triggers"
   and set an "Event-Driven" "Day timer"

function DeleteEmailsFromSenderOlderThanNDays() {
  var delayDays = 2                    // Specify the number of days before messages are deleted (moved to trash). Ex: 7 
  var Sender = ""; // specify the sender's email address. Ex:
  var maxDate = new Date(); 
  var threads =;
  for (var i = 0; i < threads.length; i++) {  
    if (threads[i].getLastMessageDate()<maxDate){  
      Utilities.sleep(1000) ;