gmail how to delete all emails before a certain date

Gmail: How to Delete All Emails Before a Certain Date

By Joshua Lee
AD

Do you find yourself drowning in an endless sea of emails in your Gmail inbox? While Gmail's powerful search features can help you find specific messages, deleting all emails before a certain date can be a challenge. In this guide, we'll walk you through a step-by-step process to help you declutter your inbox efficiently and safely.

Before we dive in, it's essential to note that Gmail doesn't have a built-in feature to delete all emails before a specific date. However, with a bit of creativity and some Google Sheets magic, we can achieve this in a few simple steps.

Preparing your Gmail Account

Before you start, make sure to enable the IMAP protocol in your Gmail settings. This will allow Google Sheets to access your email data.

AD
How to delete all mail at once on Gmail: A complete guide for 2026
How to delete all mail at once on Gmail: A complete guide for 2026
How to delete all emails on Gmail (in minutes, not hours)
How to delete all emails on Gmail (in minutes, not hours)
a pink sign with the words how to delete lots of emails at one time in email
a pink sign with the words how to delete lots of emails at one time in email

Next, compose a new email in Gmail and click on the three dots next to the "Cc" field. Select "Import mail and contacts" and follow the prompts to create a new mailbox for the export. This will serve as a backup in case anything goes wrong.

Exporting and Filtering Your Emails

Login to your Google Drive and create a new Google Sheets document. In the top panel, hover over "Extensions" and click on "Apps Script." Delete any existing code in the "Code.gs" editor and replace it with the following:

How to Permanently Mass Delete All Emails in Gmail (Quickly) | Envato Tuts+
How to Permanently Mass Delete All Emails in Gmail (Quickly) | Envato Tuts+
How to delete all emails in Gmail
How to delete all emails in Gmail

    function fetchMails() {
        var threads = Gmaillists.getTreads(service, "inbox");
        ['subject', 'from', 'to', 'date','labelIds'].forEach(key => {
            thread.forEach(thread => thread.GetmessageById(thread.getId()).get(key);)
            messages.push(m);
        }
    }

Filtering Emails by Date

To delete emails before a certain date, we'll add a filter in Google Sheets. In a new sheet, paste the following formula and adjust the date as needed:

AD

'=FILTER(concatenated_data, REGEXMATCH(date, "^20\d{2}-\d{2}-\d{2}"))'

This formula will filter out emails sent before the specified date.

Deleting the Filtered Emails

Finally, to delete the filtered emails, you can use the Apps Script again. Here's an updated version of the code that will delete the filtered emails:

AD

    function deleteMails() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName('Filtered Data');
        var data = sheet.getDataRange().getValues();
        var service = GmailApp.getGmailApp();
        for (var i = 1; i < data.length; i++) {
            var messageId = data[i][0];
            service.deleteMessage(messageId)
        }
    }

Running this script will delete all the filtered emails from your Gmail inbox.

And there you have it! You've successfully deleted all emails before a certain date in your Gmail inbox. Remember to keep your emails backed up and always double-check your actions to prevent any data loss. Happy decluttering!

AD
AD