15

I'm on a list-serv that gets emails from crons for several hundred servers. The volume of emails is about 1G/week, so to keep from bumping up against my 15G disk quota, I have to delete messages periodically. I have a filter in Gmail that adds the label 'label:inbox-zbark' to these.

I use a search like this to find a week's worth of emails:

label:inbox-zbark before:2018/12/16 after:2018/12/09

Then I can select a week's worth of messages by using the 'select all' button (which chooses the 100 conversations on the current page), then clicking the 'Select all conversations that match this search' button. I click the delete button and confirm the bulk action.

The problem is that above about 1000 conversations (which may have 100 emails a piece) gmail will either refuse to delete messages, or will throw an oops error. I can usually delete conversations 100 at a time, but this is very time consuming, and, at a certain point, gmail becomes markedly less responsive.

Gmail's trash folder has similar issues. At a certain point, the 'empty trash' button stops working... I can click the button, gmail will report All messages have been deleted., but the messages still show in the trash folder.

I'm looking for a better way to mass delete emails, doing it through inbox is getting painful.

0

3 Answers 3

10
  1. Go to https://script.google.com

  2. Create a new script

    ​function deleteOldEmail() {
      var batchSize = 100
      while (true) {
        var threads = GmailApp.search('before:2018/10/12');
        for (j = 0; j < threads.length; j+=batchSize) {
          GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
        }
      }
    }
    
  3. Save the project and click "run".
    You will need to give the script access to your Gmail. It might take a few hours/days to complete running.

6
  • This is a fantastic answer and I'd propose that it become the accepted answer for this question, as there currently isn't one. I did make some small modifications in my usage. First, Google indicates on their docs (developers.google.com/apps-script/reference/gmail/…) that they would prefer you used the paged API that takes a starting point and a range rather than returning the entire search results, so I refactored the loop to behave that way. Second, the current quotas mean that it will only run for six minutes before needing a restart. Triggers hit quotas too.
    – acm
    Sep 16, 2020 at 13:58
  • 1
    @Barton Chittenden - I encourage you to accept this answer to your question, as it is definitely the best answer here.
    – acm
    Sep 21, 2020 at 17:15
  • It shows "Exceeded maximum execution time" Dec 10, 2020 at 15:28
  • 1
    @user2335065 This function could be improved to avoid the error but the easier solution is to run it again until all the emails be deleted. You could even create a time-driven trigger to run this function every n mins where n is greater than 6 mins in case that you are using a @gmail.com account or n is greater than 30 mins is you have a G Suite / Workspace account
    – Rubén
    Jan 22, 2021 at 22:17
  • This doesn't really work unfortunately. You will eventually hit the error "Exception: Service invoked too many times for one day: gmail." after only ~10-20k emails have been deleted, and in order to get there you have to manually restart the script several times when you get the error "Exceeded maximum execution time". A better solution is needed for deleting 100k+ emails. Jan 23, 2021 at 2:38
3

Thanks @Chris for your answer which helps.

Being a JS dev, I wrote a more consistant version of it with batch actions and possible delay (in milliseconds):

function deleteBatch(searchCriteria, batchSize, delay) {
  var emails = GmailApp.search(searchCriteria, 0, batchSize);
  if (emails.length) {
    console.log(`${emails.length} found. Moving to trash...`);
    GmailApp.moveThreadsToTrash(emails);
    Utilities.sleep(delay);
    deleteBatch(searchCriteria, batchSize, delay);
  }
}

deleteBatch('label:inbox-zbark before:2018/12/16 after:2018/12/09', 100, 1000);

0

I don't know if you fixed this or not, but I was running into the same problem. If I choose to "move" the conversations into trash rather than delete them, it works for me. Hope it helps.

Your Answer

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

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