Skip to content

Instantly share code, notes, and snippets.

@peterfoxflick
Created May 16, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterfoxflick/df1eacf981074021f5fe3601434198ab to your computer and use it in GitHub Desktop.
Save peterfoxflick/df1eacf981074021f5fe3601434198ab to your computer and use it in GitHub Desktop.
If you need a simple way to remove files from a cluttered folder in canvas this should help do the trick. It only works in chunks of 100. Run with node.js
var Request = require("request");
const folderID = "XXXX"
const access_token = "XXXXXXXXXX"
delete100()
//Get all the files in a given folder ID and remove them.
function delete100(){
var url = "https://byui.instructure.com/api/v1/folders/" + folderID + "/files?access_token=" + access_token + "&per_page=100"
Request.get(url, (error, response, body) => {
if(error) {
return console.dir(error);
}
//console.dir(JSON.parse(body));
var data = JSON.parse(body)
var ids = []
data.forEach(d => {
var del = false;
//Add conditions on which type of file you want to delete
if(d.filename.indexOf(".xml") > 0)
del = true
if(d.filename.indexOf(".dif") > 0)
del = true
if(d.filename.indexOf(".wiki") > 0)
del = true
if(d.filename.indexOf(".properties") > 0)
del = true
if(del) {
deleteFile(d.id)
}
})
});
}
//A helper function that deletes and individual file.
function deleteFile(id){
console.dir("deleting file: " + id)
var url = "https://byui.instructure.com/api/v1/files/" + id + "?access_token=" + access_token;
Request.del(url, (error, response, body) => {
if(error) {
return console.dir(error);
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment