Gmail – Trouble bulk deleting mail from gmail

gmailgoogle-apps-script

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.

Best Answer

  • Go to https://script.google.com
  • 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)); } } }

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.