- An XML file that contains the code below is available to download
- box-file-delete.xml (C) Questetra, Inc. (MIT License)
- If you are using Professional, you can modify the contents of this file and use it as your own add-on
main();
function main() {
const fileIds = engine.findDataByNumber(configs.get("conf_FileIds"));
if (fileIds === "" || fileIds === null) {
throw "File IDs aren't set.";
}
let linesArray = fileIds.split("\n");
linesArray = linesArray.filter(lines => lines !== ""); // 空文字列を削除
if (linesArray.length === 0) {
throw "File IDs aren't set.";
}
const numOfLines = linesArray.length;
if (numOfLines > httpClient.getRequestingLimit()) {
throw "Number of File IDs is over the limit."
}
const oauth2 = configs.get("conf_OAuth2");
for (let i = 0; i < numOfLines; i++) {
deleteFile(oauth2, linesArray[i])
}
}
function deleteFile(oauth2, fileId) {
const url = `https://api.box.com/2.0/files/${fileId}`;
const response = httpClient.begin()
.authSetting(oauth2)
.delete(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status >= 300) {
engine.log(responseTxt)
throw `Failed to delete. File ID: ${fileId}, status: ${status}`;
} else {
engine.log(`Succeeded to delete. File ID: ${fileId}`);
}
}