Sarmad Digital
Google Services

Find and Delete Duplicate Files in Google Drive

Find and Delete Duplicate Files in Google Drive using google app script for free

Sarmad Gardezi Author Sarmad Gardezi
Find and Delete Duplicate Files in Google Drive

Introduction

To begin, it’s important to understand that this guide focuses on identifying files with the same name in Google Drive. The script provided does not check the content of the files. Here’s what the script will do:

Google Apps Script

Go to your script project home and create a new script. Add these codes there after emptying the code.gs file.
  • Target a specific folder.
  • Check the contents of the folder.
  • Compare the names and sizes of the files.
  • Consider files as duplicates if both the name and size match.
  • Remove the duplicate files if they exist.
  • Create a trigger to automate this task.
// Add id of the folder to check for duplicate
// Add id of the folder to check for duplicate
const FOLDER_ID = "Your Folder ID";

/**
 * Function looks for duplicate file names in designated folder and removes them.
 * @param {String} fileName
 */
function removeDuplicateFile() {
  let folder = DriveApp.getFolderById(FOLDER_ID);

  let files = folder.getFiles();

  let fileList = [];

  // if no file is found return null
  if (!files.hasNext()) {
    return;
  }

  // else
  while (files.hasNext()) {
    let file = files.next(),
      name = file.getName(),
      size = file.getSize();

    // checking this way always leaves first file not deleted
    if (isDuplicateFile(fileList, name, size)) {
      file.setTrashed(true);
    } else {
      fileList.push([name, size]);
    }
  }
}

/**
 * Function is helper function of removeDuplicateFile function.
 * It checks if theres already a file in the given lst with same name and size and returns true or false
 * @param {List} lst
 * @param {String} name
 * @param {Number} size
 * @returns {Boolean}
 */
function isDuplicateFile(lst, name, size) {
  for (let i = 0; i < lst.length; i++) {
    if (lst[i][0] === name && lst[i][1] === size) return true;
  }
  return false;
}


/**
 * Delete all the triggers if there are any
 */
var deleteTrigger = () => {
  let triggersCollection = ScriptApp.getProjectTriggers();
  if (triggersCollection.length <= 0) {
    console.log(`Event doesnot have trigger id`);
  } else {
    triggersCollection.forEach((trigger) => ScriptApp.deleteTrigger(trigger));
  }
  return;
};

/**
 * Create a trigger function for file which also deletes previous triggers if there are.
 */
function removeDuplicateFileTrigger() {
  // First Delete existing triggers
  deleteTrigger();

  // now remove duplicate files 
  removeDuplicateFile();
}
 
 
You can find your folder’s ID from the URL when you’re in your folder in google drive as highlighted in the image below.

Apps Script

  1. Create a New Script
    • Go to your script project home and create a new script.
    • Add the following code to the code.gs file after clearing any existing content.
  2. Find Your Folder ID
    • Navigate to your folder in Google Drive and find the folder’s ID from the URL, as highlighted in the image below.
    • For non-coders: Locate the line in the code → const FOLDER_ID = “”. Add your folder ID inside the quotes.
 
 

Setup Trigger

  1. Set Up a Time-Based Trigger
    • Follow the steps outlined in the image provided above.
     Findtrigger in google app script
    Steps to Set Up the Trigger:
    • In your script, click on the clock button on the left panel.
    • Add the removeDuplicateFileTrigger function in step 2.
    • In step 3, choose a time-driven event.
    • In step 4, select options such as hourly, weekly, or monthly to determine when the trigger should run.
    • Choose the time, which will vary based on your selection in step 4.
    • Click “Save” and you’re done!

Let’s Grow Your Revenue Through Digital Presence.

Ready to boost your business? 🚀 Let us help you grow with the power of a strong digital presence. Enter your email below, and i'll send you all the details you need to get started right away!

130+ Brands Startup

From Scratch

114 Websites

WP, React, NextJs

44 SEO Projects

On 1-3 Search Results

[contact-form-7 id="6028732" ]
Your data is complely secured with us. We don’t share with anyone.