Skip to content

Instantly share code, notes, and snippets.

@rgdelato
Created October 27, 2016 17:20
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 rgdelato/f74a89e4ce0c3d99606b6f4e8caea03d to your computer and use it in GitHub Desktop.
Save rgdelato/f74a89e4ce0c3d99606b6f4e8caea03d to your computer and use it in GitHub Desktop.
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