Gmail Search by Size with Google Apps Script

Published in: Gmail - Google Apps Script

This Google Apps Script will help you find all Gmail messages that have file attachments greater than 1 MB. Should be useful when you are running out of space in Gmail.

Also see: Sort Gmail Messages by Size

function Scanning_Gmail_Mailbox() {

  if (!UserProperties.getProperty("start")) {
    UserProperties.setProperty("start", "0");

  var start   = parseInt(UserProperties.getProperty("start"));
  var sheet   = SpreadsheetApp.getActiveSheet();
  var row     = getFirstRow();
  var ss      = SpreadsheetApp.getActiveSpreadsheet()

  for (;;) {

  ss.toast("Now finding all the big emails in your Gmail mailbox. Please wait..", "Scan Started", -1);

  // Find all Gmail messages that have attachments
  var threads ='has:attachment larger:1m', start, 100);

  if (threads.length == 0) {
    ss.toast("Processed " + start + " messages.", "Scanning Done", -1);

  for (var i=0; i<threads.length; i++) {

    var messages = threads[i].getMessages();
    UserProperties.setProperty("start", ++start);

    for (var m=0; m<messages.length; m++) {

      var size = getMessageSize(messages[m].getAttachments());

     // If the total size of attachments is > 1 MB, log the messages
     // You can change this value as per requirement.

      if (size >= 1) {
        sheet.getRange(row,1).setValue(Utilities.formatDate(messages[m].getDate(),"GMT", "yyyy-MM-dd"));
        var id = "" + messages[m].getId();
        sheet.getRange(row,5).setFormula('=hyperlink("' + id + '", "View")');

// Compute the size of email attachments in MB

function getMessageSize(att) {
  var size = 0;
  for (var i=0; i<att.length; i++) {
    //size += att[i].getBytes().length;
    size += att[i].getSize(); // Better and faster than getBytes()
  // Wait for a second to avoid hitting the system limit
  return Math.round(size*100/(1024*1024))/100;

// Clear the content of the sheet

function Clear_Canvas() {
  UserProperties.setProperty("start", "0");
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(2,1,sheet.getLastRow(), 5).clearContent();
  SpreadsheetApp.getActiveSpreadsheet().toast("Choose Scan Mailbox to continue..", "Initialized", -1);

// Find the first empty row to start logging

function getFirstRow() {
  var sheet  = SpreadsheetApp.getActiveSpreadsheet();
  var values = sheet.getRange('A:A').getValues();
  var c = 2;
  while ( values[c][0] != "" ) {
  return c;

// Add a Gmail Menu to the spreadsheet

function onOpen() {
  var menu = [
    {name: "Reset Canvas", functionName: "Clear_Canvas"},
    {name: "Scan Mailbox", functionName: "Scanning_Gmail_Mailbox"}

  SpreadsheetApp.getActiveSpreadsheet().addMenu("Gmail", menu);
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube