2

I have thousands of files on google drive which i have deleted sent to trash. My problem is they are still showing up after emptying the trash.

Emptying the trash simply does not work. The only way i can delete items and reduce quota is by searching for items using the is:trashed search operator permanently deleting them that way except this takes forever and i have no idea how to automate this using drives api.

I have successfully ran this from another question but it doesn't help my cause.

I am essentially after a script that lists files tagged as trashed owned by me and then permanently deletes them.

Any help greatly appreciated

Max
  • 21
  • 2

3 Answers3

2

"I have thousands of files on google drive which i have deleted sent to trash. My problem is they are still showing up after emptying the trash."

Just picking up on that second sentence, it's worth bearing in that when using the Google API (eg with rclone) or even with Google's own web interface, there is always a delay between issuing any "Empty the Bin" instruction, to Google Drive actually actioning it. And the more files that are in the Bin, and the smaller they are, the longer it will take for the Bin to be fully cleared. Thousands of very small files can take quite a while. And you have no control over when Google Drive will actually start the deletion or how fast it will work its way through the Bin.

Anyone who's familiar with Garbage Collection will understand precisely what I mean - "Ye know not at what hour the master cometh", as my minister Dad would have [annoyingly] put it :D

You can demonstrate this behavior for yourself by "removing" lots of files to the Bin, have the Bin displayed in a web browser and then issue (for example), "rclone cleanup mydrive:" which is the rclone command to empty the Google Drive Bin (where mydrive: has been configured to point to your Google Drive).

Firstly, you can observe that although the instruction has been issued, it can take a while before the Bin's list of files begins changing and at times, it can chug quite slowly through the list.

The other thing is that if you instruct the web interface to empty the bin it will immediately replace the list of files and folders with a graphic saying the Bin is empty. In fact, it's not, as you will see if you click on the "My Drive" folder and then immediately click back on the Bin again - the entire list will be redisplayed as before, minus anything that Google Drive has deleted in the interim. In the background, however, Google Drive will be revving up to delete your files.

The other, other thing is also that any further files and folders that are removed to the Bin after the initial "Empty the Bin" instruction has been issued, will need a subsequent command for them to be deleted permanently - they will not be covered by the first command. Again, you can demonstrate this to yourself by removing items to the Bin while the first "Empty the Bin" is in progress - you will ultimately be left with a Bin containing the second lot of removals.

I just thought it was worth pointing this out (you may disagree :D ), as I too have tried to empty my Bin in the past and thought that Google Drive wasn't doing anything.

Edit: apologies for the edit. I meant to say that once you've issued the "rclone cleanup mydrive: " command, if you then use "rclone about mydrive:", one of the stats it reports is the total space used by the files in the Bin. By periodically issuing "rclone about..." you can see the amount of space used by Bin decreasing each time. This is often the better way of checking how your "Delete Forever" command is progressing since Google Drive already considers those files gone, albeit in theory rather than practice.

Baz Cuda
  • 41
  • 6
1

Cooper's method failed for me.

files.hasNext()

does not iterate through trash items. Cooper did point me at the Advanced Google Services API:

//This method requires adding the Advanced Google Services Drive API under Resources -> Advanced Google Services
//And the Google API Console[https://console.cloud.google.com/apis/library/drive.googleapis.com/]
function DeleteTrashedFiles(){
  Drive.Files.emptyTrash();
};

If you would like to see how much of your quota you deleted you can do this.

function DeleteTrashedFilesLogged(){
  // Enable Drive.About.get() fields here:
  // https://developers.google.com/apis-explorer/?hl=en_US#p/drive/v3/
  var lngBytesInTrash = Drive.About.get().quotaBytesUsedInTrash;
  var filesDrive = Drive.Files;
  Logger.log('Attempting to clean up ' + lngBytesInTrash + ' bytes');
  filesDrive.emptyTrash();
  Logger.log('Trash now contains ' + Drive.About.get().quotaBytesUsedInTrash + ' bytes');
  filesDrive = Drive.Files;
};
0

Try this:

function delTrashedFiles() {
  var files=DriveApp.getFiles();
  while(files.hasNext()) {
    var file=files.next();
    if(file.isTrashed()) {
       Drive.Files.remove(file.getId())
     }
  }
}

You will need to enable Drive API

Cooper
  • 59,616
  • 6
  • 23
  • 54