2026-09-20 · 2 min read

Automatically Delete Old Gmail Messages to Restore Fast Sync

A Gmail account that has been active for years can easily hold more than 500,000 messages. That volume is what makes Thunderbird, Outlook or a smartphone client so slow: every client has to index and synchronise the whole mailbox, which consumes bandwidth, memory and battery. Deleting messages by hand, day after day, is slow and error-prone.

Why Big Mailboxes Break Email Clients

Desktop and mobile clients keep a local copy of the mailbox. The more threads exist, the longer the first sync takes, the bigger the local database becomes, and the more often the connection drops. Gmail itself stays responsive, but IMAP clients do not. The practical fix is to keep only recent mail, for example the last 90 days, and remove everything older.

A Batched Cleanup Script in Google Apps Script

Google Apps Script can run the cleanup for you, free of charge. Open script.google.com, create a new project and paste the code below. Edit the domains array and add your own addresses or domains, because only mail from those senders will be touched. The script searches for threads older than 90 days, moves them to the trash in small batches, and stops before the six-minute execution limit is reached. Every thread stays recoverable in the trash for 30 days.

JS
function deleteNewsletters3MonthsPermanent() {
  var domains = [
    "site1.com", "user@site2.com",
  ];

  var batchSize = 25;
  var totalDeleted = 0;
  var startTime = new Date().getTime();
  var maxExecutionTimeMs = 5 * 60 * 1000; // 5 minutes safety threshold

  for (var b = 0; b < domains.length; b += batchSize) {
    if (new Date().getTime() - startTime > maxExecutionTimeMs) {
      Logger.log("Execution limit approaching. Stopped safely. Permanently deleted so far: " + totalDeleted);
      return;
    }

    var chunk = domains.slice(b, b + batchSize);
    var fromFilters = chunk.map(function(d) {
      return d.indexOf('@') > -1 ? 'from:' + d : 'from:*@' + d;
    }).join(' OR ');

    var searchQuery = '(' + fromFilters + ') older_than:90d';
    var pageToken = null;

    do {
      if (new Date().getTime() - startTime > maxExecutionTimeMs) {
        Logger.log("Execution limit approaching inside pagination. Permanently deleted so far: " + totalDeleted);
        return;
      }

      // Max results allowed per list request by the API is 500
      var response = Gmail.Users.Messages.list('me', {
        q: searchQuery,
        maxResults: 500,
        pageToken: pageToken
      });

      if (response.messages && response.messages.length > 0) {
        var messageIds = response.messages.map(function(m) { return m.id; });
        
        // Permanent bulk delete: 50 quota units for up to 1,000 IDs
        Gmail.Users.Messages.batchDelete({ ids: messageIds }, 'me');
        totalDeleted += messageIds.length;
        
        // Small delay to keep rate pacing clean
        Utilities.sleep(600);
      }

      pageToken = response.nextPageToken;
    } while (pageToken);
  }

  Logger.log("Finished. Total messages permanently deleted: " + totalDeleted);
}

Extra Tips and a Safety Backup

Before you run anything, export your mailbox with Google Takeout so you keep a local copy. Test the script with a narrow query first, then widen it. In Thunderbird, unsubscribe from the All Mail folder and limit IMAP synchronisation to headers or to the last few days, since both changes reduce lag immediately.

Let's work together

Do you need more info, help with your project, or to develop an idea?

Whether it's an easy question, a quick doubt, or just a 5-minute chat, send me a message—it costs nothing and I'm always ready to help. I love discussing a problem to understand it, getting creative with solutions, and focusing on simple, reliable, and straightforward ideas that we can actuate quickly.

Contact me →

Switch Topic

Choose a specialized topic to explore: