Created
October 27, 2016 17:20
-
-
Save rgdelato/f74a89e4ce0c3d99606b6f4e8caea03d to your computer and use it in GitHub Desktop.
Node script to delete all Slack files older than 30 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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