Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node script to delete all Slack files older than 30 days
const https = require('https');
const token = '<get an API token here: https://api.slack.com/docs/oauth-test-tokens>';
const ts_to = Math.floor(Date.now() / 1000) - (30 * 24 * 60 * 60);
console.log(`Making API call to: https://slack.com/api/files.list?token=${token}&ts_to=${ts_to}&count=1000`);
https.get(`https://slack.com/api/files.list?token=${token}&ts_to=${ts_to}&count=1000`, (res) => {
let file_data = '';
res.on('data', (chunk) => { file_data += chunk; });
res.on('end', () => {
const response = JSON.parse(file_data);
console.log(`Found ${response['files'].length} files...`);
response['files'].forEach((file) => {
https.get(`https://slack.com/api/files.delete?token=${token}&file=${file.id}`, (res) => {
let delete_data = '';
res.on('data', (chunk) => { delete_data += chunk; });
res.on('end', () => {
const response = JSON.parse(delete_data);
console.log(file.id, response);
});
}).on('error', (e) => {
console.error(e);
});
});
});
}).on('error', (e) => {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment