Is there a script to empty Google Team Drive Trash Related Folders?

How can I make a script to empty the google team drive trash folder? If I have three main folders in google drive, it makes three main trash folders. How can I empty all those folders?

All the scripts that I found only manage to empty the my drive trash folder.

I have already tried a lot of scripts as you can see the codes below

function createTimeDrivenTriggers() {
  ScriptApp.newTrigger('emptyThrash')
      .timeBased()
      .everyMinutes(1)
      .create();
}
function emptyThrash()
{
Drive.Files.emptyTrash();

}


// I have also tried the script below
function doGet() {
  try{
  authorize();    
  var key = "YOUR DEVELOPER KEY";
  var params = {method:"DELETE",
                oAuthServiceName: "drive",
                oAuthUseToken: "always"
               };  
  UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/trash?key="+key,     params);  
  }
  catch(error)
  {
    MailApp.sendEmail("<some email>", "EMPTY TRASH BIN ERROR:<br>"+error);    
    return;
  } 
}

function authorize() {
  var oauthConfig = UrlFetchApp.addOAuthService("drive");
  var scope = "https://www.googleapis.com/auth/drive";
  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?     scope="+scope);
  oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");    
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");  
}

Upvotes: 0

Views: 3026

Answers (1)

ziganotschka
ziganotschka

Reputation: 26836

It is not possible to empty the google team drive trash folder directly.

What you can do instead is make use of the Advanced Drive Service to list the trashed contents of the team drive(s) and then permanently delete those contents:

function myFunction() { 
  var optionalArgs={'driveId':'THE TEAM DRIVE ID', 'includeItemsFromAllDrives':true, 'corpora': 'drive', 'supportsAllDrives': true, 'q':'trashed = true' }  
  var trashed=Drive.Files.list(optionalArgs).items; 
  for(var i=0;i<trashed.length;i++){
    Drive.Files.remove(trashed[i].id, {'supportsAllDrives':true}) 
  }
}

For team drives, make sure to set 'includeItemsFromAllDrives':true, 'corpora': 'drive', 'supportsAllDrives': true for listing and 'supportsAllDrives':true for removing files. To query for trashed files only, use 'q':'trashed = true'.

Be aware that the contents will be deleted for all shared drive members and to delete them you need to have the proper permissions (be a manager or content manager).

Upvotes: 2

Related Questions