Box: Delete Folder

Box: Delete Folder
Delete folders on Box. You can delete multiple folders at once. When you delete multiple ones, you should write one folder ID on each line.
2019-08-08 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Data Item with Folder IDs to delete *
Script
main();
function main(){
  const folderIds = engine.findDataByNumber(configs.get("FolderIdsItem"));
  if (folderIds == null) {
    throw "Folder IDs aren't set.";
  }
  const linesArray = folderIds.split("\n");
  const numOfLines = linesArray.length;
  if (numOfLines > httpClient.getRequestingLimit()){
  	throw "Number of File IDs is over the limit."
  }
  const token = httpClient.getOAuth2Token(configs.get("OAuth2"));
  for (let i = 0; i < numOfLines; i++){
    deleteFolder(token, linesArray[i])
  }
}
function deleteFolder(token, folderId) {
  const url = 'https://api.box.com/2.0/folders/' + folderId;
  let response = httpClient.begin()
    .bearer(token)
    .queryParam("recursive","true")
    .delete(url);
  const status = response.getStatusCode();
  if (status >= 300) {
    const error = "Failed to delete:" + folderId + "\n" + status + "\n" + response.getResponseAsString();
    throw error;
  }else{
    engine.log("Folder ID:" + folderId + "\n" + "status:" + status + "\n"+"delete successful" + "\n");
  }
}

Download

Capture

Notes

  1. Folder ID is contained in the URL. https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (60 days, as of July 2019. https://box-content.readme.io/docs/oauth-20)
%d bloggers like this: