How to clean up Gmail inbox [Detailed Guide with Automation]

How to clean up Gmail inbox [Detailed Guide with Automation]

Gmail offers 15 GB quota on free mailboxes and definitely, you will need to clean up your emails to get rid of unwanted data and free up storage.

There are several ways to clean up your Gmail inbox:

  1. Use the search function to find specific emails or types of emails, and then delete them in bulk.
  2. Use Gmail’s “star” system to mark important emails, and then use the search function to find and delete all emails that are not starred.
  3. Create filters to automatically label, archive, delete, or forward incoming emails based on specific criteria, such as sender or subject line.
  4. Use the “Archive” button to remove emails from your inbox without deleting them, so they can be accessed later through the “All Mail” label.
  5. Use the “Trash” button to delete emails that you no longer need.
  6. Unsubscribe from email lists or newsletters that you no longer want to receive.
  7. Organize your emails into folders or labels to keep your inbox more organized and easier to manage.
  8. Regularly review and delete any emails that are no longer relevant or necessary.

How to clean your Gmail Inbox using an automation

Step 1: Find all emails that are bigger in size

At first, let’s find out all emails that are bigger in size.

To get a list of all emails with size more than 5 MB in a Google Sheets document using Google Apps Script, you can follow these steps:

  1. Set up the script: Create a new Google Apps Script project and open the Script Editor.
  2. Authorize the script: In the Script Editor, click the “Run” button and follow the prompts to authorize the script to access your Google account and Gmail.

Here is the full code for getting a list of all emails with size more than 5 MB in a Google Sheets document using Google Apps Script:

// Get the ID of the sheet
var sheetId = "YOUR_SHEET_ID";

// Get a reference to the sheet
var sheet = SpreadsheetApp.openById(sheetId).getSheets()[0];

// Get a list of all emails
var emails = GmailApp.search('size:5m');

// Write the email details to the sheet
for (var i = 0; i < emails.length; i++) {
  var email = emails[i];
  sheet.appendRow([email.getSubject(), email.getFrom(), email.getDate(), email.getId()]);
}

To use this code, you will need to replace “YOUR_SHEET_ID” with the ID of the sheet where you want to store the list of emails. You will also need to authorize the script to access your Google account and Gmail.

This code will get a list of all emails that are larger than 5 MB and write the subject, sender, and date of each email to a row in the sheet. You can modify the code to include additional email details or to use a different search criteria to filter the emails.

Step 2: Delete all emails that match the list of message IDs retrieved in the last script

To delete all emails in Gmail that match a list of message IDs using Google Apps Script, you can use the following steps:

  1. First, you need to retrieve the message IDs from the Google Sheet. You can do this using the getRange and getValues methods of the Spreadsheet class. For example:
// Get the range of cells that contain the message IDs
var range = SpreadsheetApp.getActiveSheet().getRange(4, 4);

// Get the values in the range as a 2D array
var values = range.getValues();
  1. Next, you can retrieve the messages that match the message IDs. You can use the Gmail.Users.Messages.list method for this, which takes a list of message IDs as one of its parameters. This method returns a list of message objects, each of which contains information about a specific message, such as its subject and sender.
  2. Finally, you can loop through the list of message objects and delete each message using the Gmail.Users.Messages.delete method. This method takes a message ID as its parameter, so you can pass the ID of each message to be deleted.

Here is some sample code that demonstrates how to delete all emails in Gmail that match a list of message IDs stored in a Google Sheet:

function deleteEmails() {
  // Get the range of cells that contain the message IDs
  var range = SpreadsheetApp.getActiveSheet().getRange(4, 4);
  
  // Get the values in the range as a 2D array
  var values = range.getValues();
  
  // Flatten the 2D array into a 1D array
  var messageIds = values.flat();
  
  // Retrieve the messages that match the message IDs
  var messages = Gmail.Users.Messages.list('me', { ids: messageIds });
  
  // Loop through the list of messages and delete each one
  for (var i = 0; i < messages.length; i++) {
    var message = messages[i];
    Gmail.Users.Messages.delete('me', message.id);
  }
}

Note that this code assumes that you have authorized the Gmail API and that you have included the Gmail library in your script. You will also need to replace 'me' with the email address of the account you want to delete the messages from.