How to Overwrite Existing Files in Google Drive

Published in: Download Gmail to Google Drive

The premium version of Save Gmail addon offers an option to overwrite existing files in Google Drive instead of creating a new file.

For instance, a sales company receives disk backups in zip format every midnight in their Gmail account. They use the addon to automatically download this file attachments to a specific folder in Google Drive. However, instead of creating multiple copies of the backup, they just want the program to overwrite the existing file and not create a new file.


Go to the add-on menu inside the Google Spreadsheet, choose Save Emails > Create Rule and check the option that says “Overwrite attachments”. Now if you receive another email attachment with the same name as the one that already exists in the current folder, the addon will simply replace the file content without changing the name, location or description of the file. Even the old file links would continue to work.

How to Overwrite Files in Google Drive with Google Apps Script

Internally, the add-on uses the Advanced Drive API service of Google Script to overwrite existing files by replacing the content.

Here’s a sample snippet that downloads a Gmail attachment to Google Drive but creates a new file only if none exist.

function overwriteFilesInGoogleDrive(messageId, folderId) {

// Get the Gmail Message var message = GmailApp.getMessageById(messageId);

// Get the parent Google Drive folder var folder = DriveApp.getFolderById(folderId);

// Get the first Gmail attachment

var att = message.getAttachments()[0]; var filename = att.getName();

// Find if files of the same name exist in that folder var existing = folder.getFilesByName(filename);

// Does file exist? if (existing.hasNext()) {

var file =;

// Make sure the file name is exactly the same
if (file.getName() === filename) {

  // Updates file metadata and/or content with the Drive API
    title: file.getName(), mimeType: file.getMimeType()
  }, file.getId(), att.copyBlob());

} }

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Download Gmail to Google Drive

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