Skip to content

Instantly share code, notes, and snippets.

@gunar
Last active August 2, 2024 12:25
Show Gist options
  • Save gunar/cafb5dd60cac12c8f6c2665852bfb69d to your computer and use it in GitHub Desktop.
Save gunar/cafb5dd60cac12c8f6c2665852bfb69d to your computer and use it in GitHub Desktop.
Remove Duplicates From GDrive Root
/*
Gunar C. Gessner
@gunar 2016
INSTALL: https://script.google.com/macros/s/AKfycbym7IaTORzJ7LQPcxMx1LV1SQEC6RVGv5tzLOgYS8iQe8XAJxM/exec
After installation, the script will, every 10 minutes, search for all files that have duplicates (shift+z) and remove them from the root directory ("My Drive").
This Google Apps Script webapp aims to solve the problem that when transferring ownership of folders &
files to another account, duplicate files shown in "My Drive".
Original Google Drive Help forum issue: https://productforums.google.com/forum/#!topic/drive/Ldz7nvSXQ6w
*/
'use strict'
function updateTriggers(triggerId) {
// Remove all project Triggers
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
// Add single one
ScriptApp.newTrigger('removeDuplicatesFromRoot')
.timeBased()
.everyMinutes(10)
.create();
}
function doGet() {
updateTriggers();
return HtmlService.createHtmlOutput('Script successfully installed! It will remove duplicates every 10 minutes.');
}
function removeDuplicatesFromRoot() {
Logger.clear();
var rootFolder = DriveApp.getRootFolder();
var files = rootFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
var isInMyDrive = false;
var isSomewhereElse = false;
while (parents.hasNext()) {
var folder = parents.next();
if (folder == 'My Drive') { isInMyDrive = true }
else { isSomewhereElse = true }
}
Logger.log('===' + file + ': ' + isInMyDrive + '/' + isSomewhereElse);
if (isInMyDrive && isSomewhereElse) { rootFolder.removeFile(file); }
}
}
@HE-MAN96
Copy link

What should i do if i want to delete duplicate files from some other folder inside my drive?

@AINS-ECS
Copy link

@gunar Do you have another link for the install portion at the top of your script? I would really like to use this code.

INSTALL: https://script.google.com/macros/s/AKfycbym7IaTORzJ7LQPcxMx1LV1SQEC6RVGv5tzLOgYS8iQe8XAJxM/exec

@CraftCanna
Copy link

Is there a working script still?

@gunar
Copy link
Author

gunar commented Aug 2, 2024

I guess one could copy the removeDuplicatesFromRoot() function into a new Google Apps Script file then run it. I'm not sure if the script still works though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment