Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script to delete all files from google drive?

I need to delete all files from my drive, more than 16 GB and I'm getting hours manually deleting.

Sought help in support of google and had no help.

Can I move a Google Apps Script that I execute?

like image 268
André Páscoa Avatar asked Oct 27 '13 04:10

André Páscoa


People also ask

How do I mass delete files from Google Drive?

Here's how to delete multiple files from Google Drive on Android. Hold down on the first file to select it. After this& tap on all the files you need to delete. Look to the top of the screen& and you will see the Trash icon& tap on it to move all the files to the trash.

How do I mass delete shared files in Google Drive?

To pick a file, you may click it once and then hit the "Delete" button at the top. You may also remove multiple files simultaneously. Click the first file and click on other files to select them while holding down the "Ctrl" or "Command" key. Click the "Remove" button to remove all your selected files.

How do you delete a file in an app script?

Delete a fileOpen your Apps Script project. At the left, click Editor code. Next to the file you want to delete, click More more_vert > Delete.


2 Answers

I was very interrested by patt0's (best) answer and tried to improve it (just a little :-) by adding a few features for my personal comfort...

Here is what I came to, just for information (added data logging saved in a single document that won't be deleted so you can keep a trace of what happened - or what will happen if you run it with the commented setTrashed()- and sending a mail to you with the log data doc url for easy access)

function processAllFiles() {
  var continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
  var numberOfFiles = Number(UserProperties.getProperty('Number_of_files_processed'));
  var thisScriptFileId = DocsList.find("continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN')")[0].getId();
  Logger.log(thisScriptFileId);
  if(UserProperties.getProperty('logFileId') == null ){
    var logFileId = DocumentApp.create('Delete All Files Log data').getId();
    var doc = DocumentApp.openById(logFileId);
    doc.getBody().appendParagraph('List of all the files you deleted\n\n');
    UserProperties.setProperty('logFileId', logFileId);
  }
  if (continuationToken == null) {
    var files = DriveApp.getFiles();
    var continuationToken = files.getContinuationToken();
    UserProperties.setProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN', continuationToken);
    UserProperties.setProperty('Number_of_files_processed', '0');
  } else {
    var files = DriveApp.continueFileIterator(continuationToken);
  }

   while (files.hasNext()) {
     var file = files.next();
     if(file.getId()!=logFileId&&file.getId()!=thisScriptFileId){
//     file.setTrashed(true);
       numberOfFiles++
         Logger.log('File '+Utilities.formatString("%05d", numberOfFiles)+' : '+file.getName());
     }
   }
  var paragraphStyle = {};
  paragraphStyle[DocumentApp.Attribute.FONT_SIZE] = 8 ;

  var doc = DocumentApp.openById(UserProperties.getProperty('logFileId'));
  doc.getBody().appendParagraph(Logger.getLog()).setAttributes(paragraphStyle);
  MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'DeleteFiles result Log','Here is the log data to your script :\n\n'
                    +doc.getUrl()+'\n\nExecuted by this script : '+DocsList.getFileById(thisScriptFileId).getUrl());
  // finish processing delete the token
  UserProperties.deleteProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
  UserProperties.deleteProperty('Number_of_files_processed');
}
like image 67
Serge insas Avatar answered Sep 18 '22 16:09

Serge insas


I am going to assume you are familiar with Google Apps Script to the point where you know how to create a script in your drive, manage the editor etc ... if you are not please start here https://developers.google.com/apps-script/overview.

Here a little script that will list all your files and set them to the trash, you will still need to go to trash and delete forever.

BE CAREFUL WHEN USING THIS SCRIPT : MOVES ALL FILES TO TRASH

You will need to uncomment the file.setTrashed(true) when you run this

function processAllFiles() {
  // we look for the continuation token from the UserProperties
  // this is useful as the script may take more that 5 minutes 
  // (exceed execution time)
  var continuationToken = UserProperties.getProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');

  if (continuationToken == null) {
    // firt time execution, get all files from drive
    var files = DriveApp.getFiles();
    // get the token and store it in a user property
    var continuationToken = files.getContinuationToken();
    UserProperties.setProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN', continuationToken);
  } else {
    // we continue to execute (and move everything to trash)
    var files = DriveApp.continueFileIterator(continuationToken);
  }

   while (files.hasNext()) {
     var file = files.next();
//     file.setTrashed(true);
     Logger.log(file.getName());
  }

  // finish processing delete the token
  UserProperties.deleteProperty('DELETE_ALL_FILES_CONTINUATION_TOKEN');
}

You might potentially be left with very many folders (if they were created programatically for some reason ;) ) so you could run this little script to move them to the trash aw well. Don't forget to uncomment the line that counts below.

function processAllFolder() {
// Log the name of every folder in the user's Drive.
  var folders = DriveApp.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
     Logger.log(folder.getName());
     // folder.setTrashed(true);
  }
};

Let me know how that works out for you.

like image 37
patt0 Avatar answered Sep 21 '22 16:09

patt0