What do you do when your Gmail account storage is nearly full? You can either purchase additional storage space from Google – they charge $5 per year for 20 GB – or a cheaper alternative would be that you scan your Gmail mailbox for messages that contain large file attachments and delete (or forward) all the bigger messages and recover precious space.

The problem is how do you find these bulky messages in your mailbox when Gmail doesn’t offer an option to sort and filter messages by size?

In the past, I have shared both software tools and web apps that help you quickly track the space-hogging emails in your Gmail account but there’s a better option – you can use Google Scripts inside Google Docs to find all the space-hogging attachments inside Gmail in 2 minutes.

gmail sort by size

Sort GMail Messages by Size of Attachments

The idea is that your Google Docs will connect to your Gmail account and compute the size of every message that’s present in your mailbox. If it finds a bulky message (size > 1 MB), it will make a note of it in the spreadsheet.

Once the sheet has a list of all the bulky message, you can sort the sheet by the Size column to find the big ones. Or use the Filter option (the Funnel icon) to find messages that are within a particular range (5 MB < size < 10 MB). Click the “View” link to open the corresponding message in Gmail, forward it to a secondary email address and delete it from the primary Inbox to recover space.

Here’s how you can get started:

  1. Create a copy of this sheet in your Google Docs account.
  2. A new Gmail Menu will appear in the sheet after about a minute. Select “Reset Canvas” from the Gmail menu to initialize your sheet.
  3. Accept the authorization screen and then choose Grant Access to let Google Docs access your Gmail Inbox. This is completely safe because your own Google Docs account is requesting access to your own Gmail account (see source code).
  4. Once the permissions have been granted, choose “Scan Mailbox” from the Gmail menu to start the scanning process.

Sit back and relax as the last step may take time depending on how big your Gmail mailbox is.

If your mailbox is big, the program may time-out. No problem, choose the same Google sheet, select “Scan Mailbox” from the Gmail menu again and the script will resume scanning from where it left off.

Awesome Google Scripts → Custom Google Scripts →

Once all the mails have been analyzed, use the sort or filter options in Google Docs to find the biggest emails, apply a common label and then use bulk auto-forward to send all these messages to your secondary email address. That’s it!

Troubleshooting tips: If you get an error that says “Service invoked too many times for one day” or “Exceeded maximum execution time”, you may want to wait for some time before re-running the program. These are Google Apps Script limits to prevent abuse.

Scan Gmail Messages with a trigger

If your Gmail mailbox is large, you can also set up a time-driven trigger to let the script automatically without manual intervention. In this case, you can even close the browser tab and the script will run in the background. Here’s how:

  1. While the Google sheet is open, go to Tools -> Script Editor. On the next screen, choose Resources -> Current Script Triggers.
  2. Click “Add a New Trigger,” change the Event from “Spreadsheet” to “Time Driven” and set a minutes timer that triggers every 10 minutes.
  3. Save the trigger, authorize the script if it requires and close the sheet.