Find and Delete Duplicate Files in Google Drive
Find and Delete Duplicate Files in Google Drive using google app script for free
Views: Loading...
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(); }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
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
-
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.
-
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
-
Set Up a Time-Based Trigger
- Follow the steps outlined in the image provided above.
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!