Google Apps Script to delete all files from Google Drive?

I need to delete all files from my disk, more than 16 GB, and I get the clock manually by deleting.

Help in support of Google disappeared and did not help.

Can I move the Google Apps Script that I executed?

+8
google-drive-sdk google-apps-script
source share
2 answers

I assume that you are familiar with Google Apps Script to a level where you know how to create Script on your drive, manage the editor, etc ... unless you start here https://developers.google.com/ apps-script / overview .

Here is a little Script in which all your files will be listed and install them in the trash, you still need to be permanently deleted and deleted.

BE CAREFUL WHEN USING THIS Script: MOVES ALL FILES TO SKIP

You need to uncomment the .setTrashed (true) file when running 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'); } 

Perhaps you can have so many folders (if they were created programmatically for some reason;)) so you can run this little Script to move them to the trash well. Do not forget to uncomment the line that is counted below.

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

Let me know how it works for you.

+8
source share

I was very interested in the answer patt0 (best) and tried to improve it (a little :-), adding a few functions for my personal comfort ...

Here's what I came for information only (added data logging saved in one document that will not be deleted so you can keep track of what happened or what happens if you run it with setTrashed() comments - and sends message to you with the address of the doc log data 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'); } 
+3
source share

All Articles