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

痴心易碎 提交于 2020-04-18 06:40:21

问题


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");  
}

回答1:


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).



来源:https://stackoverflow.com/questions/57764248/is-there-a-script-to-empty-google-team-drive-trash-related-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!