Skip to content

Instantly share code, notes, and snippets.

@joewright
Last active December 7, 2020 22:02
Show Gist options
  • Save joewright/18d3820f929d5773f22aa1054b78b610 to your computer and use it in GitHub Desktop.
Save joewright/18d3820f929d5773f22aa1054b78b610 to your computer and use it in GitHub Desktop.
Delete all files from a Slack workspace with a bot token and the Web API
const slack = require('@slack/web-api');
//@see https://api.slack.com/authentication/token-types#bot
const slackClient = new slack.WebClient('<your-bot-token>');
//start deletin'
deleteAllFiles().then(() => {
logg('All done');
}, err => {
console.error(err);
});
function logg(msg) {
console.log(`${new Date().toJSON()} ${msg}`);
}
async function deleteAllFiles() {
// @note you can start on a later page by adjusting limit/page here
let limit = 2;
for (let page = 1; page < limit; page++) {
logg(`Deleting slack files, page ${page} of ${limit}`);
//slack returns a paginated list (100 per page) of files
//@see: https://api.slack.com/methods/files.list
const filesResp = await slackClient.files.list({ page });
// remove each file in series using each file `id` value
for (let file of filesResp.files) {
const created = new Date(file.created * 1000).toJSON();
logg(`Deleting file ${file.name} ${file.id}; Size: ${file.size}; Created: ${created}`);
//@see: https://api.slack.com/methods/files.delete
await slackClient.files.delete({
file: file.id
});
}
// update page limit based on slack's response
if (filesResp.paging && filesResp.paging.pages !== limit) {
limit = filesResp.paging.pages;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment