Google Drive is a great tool for storing and organizing files, but it can be cumbersome to manage and de-duplicate files. In this article, we will explore how to delete duplicates in Google Drive with Google Apps Script.
Google Apps Script is a powerful JavaScript platform that allows for automating tasks and extending the capabilities of Google products. It can be used to enhance productivity and streamline workflow, including Google Drive management tasks like deleting duplicates.
Step 1: Find Duplicates
The first step in deleting duplicates in Google Drive is to find them. We can use the `DriveApp` class in Google Apps Script to access the files in our Drive. We can then loop through each file and compare its title, size, and/or content to the other files in the Drive to determine duplicates.
function findDuplicates() {
var files = DriveApp.getFiles();
var duplicates = [];
while (files.hasNext()) {
var file = files.next();
var title = file.getName();
var size = file.getSize();
var content = file.getBlob().getDataAsString();
var temp = DriveApp.searchFiles(
'title="' + title + '" and ' +
'mimeType="' + file.getMimeType() + '" and ' +
'trashed=false and ' +
'visible=true');
while (temp.hasNext()) {
var tempFile = temp.next();
var tempTitle = tempFile.getName();
var tempSize = tempFile.getSize();
var tempContent = tempFile.getBlob().getDataAsString();
if (tempFile.getId() !== file.getId() &&
tempTitle === title &&
tempSize === size &&
tempContent === content) {
duplicates.push(tempFile);
}
}
}
return duplicates;
}
This function finds duplicates by comparing the title, size, and content of each file in the Drive with other files of the same type that are not trashed and visible. It returns an array of the duplicates found.
Step 2: Delete Duplicates
Once duplicates are found, they can be deleted using the `DriveApp` class.
function deleteDuplicates() {
var duplicates = findDuplicates();
var numDeleted = 0;
for (var i = 0; i < duplicates.length; i++) {
var file = duplicates[i];
// Delete file
Drive.Files.remove(file.getId());
numDeleted++;
}
Logger.log(numDeleted + ' duplicates deleted.');
}
This function uses the findDuplicates()
function to get an array of duplicate files, then loops through each file to delete it using the Drive.Files.remove()
method. It logs the number of duplicates deleted in the console for reference.
Conclusion
Google Apps Script is a powerful tool for automating tasks in Google Drive, including deleting duplicates. By using the DriveApp
class and comparing file names, sizes, and/or contents, duplicates can be easily identified and removed for better organization and efficiency. This code can be modified or extended for even more advanced Drive management tasks.