11

I need to delete certain Gmail messages older than n days (I can get the whole set though a search string). I know this is not possible directly within Gmail (see for instance this question) so I am looking for a webapp which could handle such activities on my account (and, generally, similar housekeeping tasks).

An automated solution that would actively mark messages as read when they reach a specified age is the desired solution. Is there a way to accomplish this through Gmail filters and some syntax? Are there any apps, scripts, etc. that would help?

2
  • It might be possible to write a Greasemonkey script that would perform this action. Commented Jul 8, 2012 at 19:58
  • I am also interested in accomplishing this. Help wanted.
    – John
    Commented Jul 28, 2012 at 5:36

8 Answers 8

0
  1. There's no built-in feature in Gmail to do this as of writing this answer (Jan 2013).
  2. I had same issue with all these old newsletters and notifications and I have created Google app script to do exactly this, with some help from built-in filtres. Please check out here: http://2sdd.blogspot.ru/2012/11/how-to-clean-up-gmail-inbox-from-emails.html

Script is very simple, feel free to hack it to your own needs.

1
  • Thanks - also for the script which basically solves the issue (though in an indirect way, this is something which should really be implemented by Google). Updating "best answer" :)
    – WoJ
    Commented Jan 9, 2013 at 8:55
5

According to Google:

  • older_than, newer_than — Similar to older and newer, but allows relative dates using d, m, and y for day, month, and year
    Example: newer_than:2d
    Meaning: Finds messages sent within the last two days.

With this you can create a filter deleting these, and even only the mails in a specific label.

Source.

4
  • 1
    Thanks but this is not what I was looking for. I know that I can search for emails meeting certain criteria. What I want to do is to have an automated way to handle them (delete in that case), without manual actions from my side.
    – WoJ
    Commented Jan 15, 2013 at 13:28
  • 2
    @WoJ Filters are what you use to automatically delete messages meeting certain criteria. Is the problem that, even with the older_than: criteria, the messages only get actioned by the filter when entering your inbox, and so after that, the filter won't trigger when the messages become older than the given period? Commented Jan 16, 2013 at 1:10
  • 1
    @Nathan Craike: exactly. I have a bunch of filters which handle incoming mail but, as you mentionned, this is only for incoming mail, not the one in th einbox.
    – WoJ
    Commented Jan 16, 2013 at 7:43
  • This won't work. According to Google, Filter searches containing "label:", "in:", "is:", date range (e.g. "before:" or "after:") or stars criteria (e.g. "has:yellow-star") are not recommended as they will never match incoming mail.
    – DxTx
    Commented Oct 5, 2018 at 17:31
3

Why not simple search, select and delete?

Example, you want to delete all mails before 1st January 2009. So you search Gmail for -

before:2009/1/1

(You can search for anything and rest of this answer would still work)
That returns all the mails in inbox before 1st january 2009 -

enter image description here

Now you select all mails in the current page by ticking this box -

enter image description here

Google will select all mail on the page and also ask you if you want to select all the mails that matches your search query -

enter image description here

If you click Select all conversations that match this search, it'll select all the mails before 1st jan 2009, which are all the mails you want to delete.

You can then click the Delete button and it'll ask you if you are sure -

This action will affect all conversations in this search. Are you sure you want to continue?

Press Ok and your mails should be deleted.

4
  • 2
    Caution - if conversations are turned on, you will also delete any messages after that date if they are part of a conversation that occurs before the specified date. Commented May 11, 2012 at 3:46
  • Thanks but this is not what I want to achieve. I know about the search options and what I am looking for is a way to match messages "older than 10 days" (for instance). Not "older than May 1st" (in the case of today). The typical usage would be to get rid of messages which make sens only for a given time, like TV listings. This needs to be a relative time measure, not a date-based one.
    – WoJ
    Commented May 11, 2012 at 6:56
  • Do you want to create a filter? If not, it's just a matter of one step to turn "10 days older" to "before 01-05-2012" which doesn't take more than a second.
    – Bibhas
    Commented May 11, 2012 at 6:59
  • Do you know how to create a Greasemonkey script that would perform this action automatically? Commented Jul 8, 2012 at 20:01
2

If you have 10,000 emails sitting in google, deleting 8,000 of them 20 at a time is not a fun option. An easier option if you have a very large amount of email is to get a free email client such as Thunderbird and set it up to delete mail older than X number of days. If you want to keep mail longer than the limit in the email client, set it to delete from the server when deleted from the client. Then check the email with the client every so often to delete the email from the server.

2

I have something like this hacked up. I probably based it off something someone else wrote, but it was so long ago I don't remember.

It has chugged away reliably since. Here's how it works:

Generally, it looks for messages with certain tags and then replaces that tag with another and then archives them.

Specifically, messages are tagged with inbox filters to indicate how they will be "expired." In the example below this is based on how old they are, and the label is called Bulk/Expires/[Daily|Weekly|Monthly]. (Note: this is a nested tag, but they don't need to be nested, I just like to keep them organized like this). Every day some Google Apps Scripts will run to check if threads within those labels match some condition, generally a date. It will then replace that tag with another tag (called Bulk/Expired below) and archive it. You could also just have it delete the message.

This is code (with extra comments) which will clean up messages more than a day old. It's setup to trigger every day at like 4am:

function cleanUpDaily() {
  // Enter # of days before messages are archived
  var delayDays = 1 
  // make an empty Date() object
  var maxDate = new Date(); 
  // Set that date object ('maxDate')to the current data minus 'delayDays'.
  // In this case it's a date 1 day before the time when this runs.
  maxDate.setDate(maxDate.getDate()-delayDays); 
  // this is the label that finds messages eligible for this filter
  var currLabel = GmailApp.getUserLabelByName("Bulk/Expires/Daily"); 
  // this is the new label so I know a message has already been "Expired"
  var newLabel = GmailApp.getUserLabelByName("Bulk/Expired"); 
  // Get the message threads which might need to be expired.
  var threads = currLabel.getThreads(); 
  // Iterate over those threads and check if they need to be expired
  for (var i = 0; i < threads.length; i++) { 
    // You can put whatever kinds of conditions in here,
    // but this is just going to check if they were recieved before
    // 'maxDate' which here is 1 day before runtime.
    if (threads[i].getLastMessageDate()<maxDate) 
      {
        // If they're old, archive them
        threads[i].moveToArchive(); 

        // Remove the old label, they won't need to be expired again 
        // This isn't required, but it will make it slow, and Google will
        // time-out things that take too long, in my experaince it will
        // become slow and start timing out if there are more than a few
        // dozen threads to process, YMMV.
        threads[i].removeLabel(currLabel);

        // Label the thread with a new label indicating it's gone through this 
        // process. Also not strictly necessary, but it's useful if you'd like
        // to do some more processing on them in the future.
        threads[i].addLabel(newLabel); 
      }
  }
}

Here's the code for doing this for things which should expire in a week or a month, you setup triggers to run these functions either weekly or monthly.

function cleanUpWeekly() {
  var delayDays = 7 // Enter # of days before messages are moved to archive
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays);
  var currLabel = GmailApp.getUserLabelByName("Bulk/Expires/Weekly"); // this is the label that finds messages eligible for this filter
  var newLabel = GmailApp.getUserLabelByName("Bulk/Expired"); // this is the new label so I know a message was expired and thats why its archived
  var threads = currLabel.getThreads();
  for (var i = 0; i < threads.length; i++) {
    if (threads[i].getLastMessageDate()<maxDate)
      {
        threads[i].moveToArchive();
        threads[i].removeLabel(currLabel); // I take the label off so there's not an infinitely growing "threads" variable with time
        threads[i].addLabel(newLabel);
      }
  }
}

function cleanUpMonthly() {
  var delayDays = 30 // Enter # of days before messages are moved to archive
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays);
  var currLabel = GmailApp.getUserLabelByName("Bulk/Expires/Monthly"); // this is the label that finds messages eligible for this filter
  var newLabel = GmailApp.getUserLabelByName("Bulk/Expired"); // this is the new label so I know a message was expired and thats why its archived
  var threads = currLabel.getThreads();
  for (var i = 0; i < threads.length; i++) {
    if (threads[i].getLastMessageDate()<maxDate)
      {
        threads[i].moveToArchive();
        threads[i].removeLabel(currLabel); // I take the label off so there's not an infinitely growing "threads" variable with time
        threads[i].addLabel(newLabel);
      }
  }
}

Right now I'm working on one which will take the Bulk/Expired messages and if they have a Purge tag it will delete them permanently. I'm disinclined to ever delete an email (crazy), but lots of archived mailing list things tend to pollute search results. This annoyance has started to overwhelm my digital hoarding tendencies. The only change is that the for loop checks to see if a message has the 'Purge' tag. This is not trivial, because the labels a given thread has are returned as an array, and so I have to check that array which will add a few lines of code. Unless I find some slicker way.

I mainly use this to manage newsletters with Google Inbox. I setup a message bundle for the `Bulk/Expires/Daily' tag, and the filter makes sure only today's newsletter is there. Then whether I read it on a given day or not, the latest is there. It's kinda like hacking Inbox into an RSS reader. I do the same thing for regular newsletters/bulk mailings which come out weekly or monthly. Generally I expire them when their age removes their relevance.

0

Try the newer and efficient script at https://github.com/omkar9999/GmailCleaner

The script will clean thousands of old junk emails within minutes if scheduled to run every minute and will not cross Google's Daily Quota.

-1

No there is not, what I would suggest doing is something like Al Everett mentions on the question you linked to. You can have various links for the different tasks you want to perform.

It would be nice to have a way to delete email older then a few days automatically but as of right now you cannot.

-1

You can also use this google script. The advantage would be that you can give nested labels too.

Google Apps Script; delete Gmail e-mails

To run, create a copy of the script, add in values for label and "N" days, do Run-> Initialize and then Run-> Install.

1
  • A similar answer has also been posted to Lifehacker
    – WoJ
    Commented Feb 24, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.