Last active
August 13, 2018 16:53
-
-
Save Calyhre/f5da192c36147d3098770765958a5e0d to your computer and use it in GitHub Desktop.
Delete all Slack files older than 30 days, for a specific user
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'); | |
// Get your token here https://api.slack.com/custom-integrations/legacy-tokens | |
const TOKEN = ''; | |
// Find your user ID here https://api.slack.com/methods/auth.test/test | |
const USER_ID = ''; | |
const delay = 300; // delay between delete operations in millisecond | |
const baseApiUrl = 'https://slack.com/api/'; | |
const fileListApiUrl = baseApiUrl + 'files.list?token=' + TOKEN + '&count=1000'; | |
const deleteApiUrl = baseApiUrl + 'files.delete?token=' + TOKEN; | |
const thirtyDaysAgoTimeStamp = Math.round( | |
(new Date().getTime() - 1000 * 60 * 60 * 24 * 30) / 1000 | |
); | |
// --------------------------------------------------------------------------------------------------------------------- | |
function deleteFile({ id: fileId } = {}) { | |
https | |
.get(`${deleteApiUrl}&file=${fileId}`, res => { | |
let body = ''; | |
res.on('data', chunk => { | |
body += chunk; | |
}); | |
res.on('end', () => { | |
const { ok } = JSON.parse(body); | |
if (ok === true) { | |
console.log(fileId + ' deleted!'); | |
} | |
setTimeout(deleteFile, delay); | |
}); | |
}) | |
.on('error', error => { | |
console.log('Got an error: ', error); | |
}); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
https | |
.get( | |
`${fileListApiUrl}&user=${USER_ID}&ts_to=${thirtyDaysAgoTimeStamp}`, | |
res => { | |
let body = ''; | |
res.on('data', chunk => { | |
body += chunk; | |
}); | |
res.on('end', () => { | |
const { files } = JSON.parse(body); | |
files.map(deleteFile); | |
}); | |
} | |
) | |
.on('error', error => { | |
console.log('got an error: ', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment