Find and Delete Duplicate Files With In Google Drive With Google Apps Script
Tags: googleappsscript, javascript, googledrive, automation
Published: 2023-02-22 09:33:25

You might be running an organization where files are created automatically or by other employees/collaborators.  Have you been troubled by google’s feature that allows files with the same name?

Then this blog is for you. You’ll get a script to delete the duplicate files and also be able to automate the process.

Hi, this is Nibesh Khadka. **<u>I am a freelancer that helps clients with jobs related to Google Workspace Products and Google Apps Script</u>**. This blog is meant for both coders and non-coders, so, please do humor the over-explanation.

## Intro

First of all, you have to understand that we are talking about files with the same name. We won't be checking the contents of the files in the script. So, we'll write a script:

1. That'll target a folder
2. Check the contents of the folder.
3. Compare the names and sizes of files with each other.
4. If both matches then consider them a duplicate file
5. Remove the duplicate files, if they exist.
6. Create a trigger to automate this task.

## Apps Script

Let's first go to your script project [home](https://script.google.com/home) and create a new script. Add these codes there after emptying the **code.gs** file.

```
// Add id of the folder to check for duplicate
// Add id of the folder to check for duplicate
const 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();
}
```

> removeDuplicateFileTrigger() is a function that prevents installable triggers to accumulate and cause errors. If you  don’t want to automate you should run the removeDuplicateFile() function in the script instead.

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.


![Get Folder ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h5smxwzyta9nnfs8wd3.png)

_Non-coders: You can see the second line in code→ const FOLDER_ID = "". You’re supposed to add your folder id from here inside quotes._


## Setup Trigger

You can now set up a time-based trigger to automate this task.


![Create Trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjlu5po5jkw918vfrxmp.png)

Now, follow the steps indicated in the image above.

1. In your script,  on the left panel, press the clock button. 
2. Remember to add **removeDuplicateFileTrigger** function in step 2.
3. Pick time driven event in the 3rd step.
4. In 4th step, you can of course select hourly, weekly, monthly, and more options to choose when to run the trigger.
5. Choose the time which will appear different based on your selection in the 4th step.
6. Hit Save and you’re done.


> Warning: This script does not take into account the [limitation](https://developers.google.com/apps-script/guides/services/quotas) of google apps script for triggers and script run time. So, this script probably won't work for very large and old folders with many files.

## Reference

Inspired by [this](https://stackoverflow.com/questions/63366697/find-duplicate-files-in-one-folder-and-removing-the-newest-one-google-script/63367051#63367051) StackOverflow answer.

## Thank You

This is Nibesh Khadka from Khadka's Coding Lounge. Find my blogs [here](https://kcl.hashnode.dev). Please do like and share if you like my work. Also, subscribe to get notified about the next blog.

> I make Google Add-Ons and can also write Google Apps Scripts for you. If you need my services let me [know](https://nibeshkhadka.com/contact/).

Thank you for your time.