Created
October 19, 2018 02:09
-
-
Save adrianvu/a105b08a5a015fb3d75706ff86ffbdac to your computer and use it in GitHub Desktop.
Slack Bulk Delete Messages
This file contains hidden or 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
#!/usr/bin/env node | |
// 1. Get Legacy Token: https://api.slack.com/custom-integrations/legacy-tokens | |
// 2. Get Channel ID from the the browser URL.: https://workspace.slack.com/messages/CHANNEL_ID/details/ | |
// Pass it as a parameter: node ./slack-delete-channel-messages.js CHANNEL_ID | |
// CONFIGURATION ####################################################################################################### | |
const token = 'INSERT TOKEN HERE'; // You can get it from: https://api.slack.com/custom-integrations/legacy-tokens | |
// GLOBALS ############################################################################################################# | |
let channel = ''; | |
if (process.argv[0].indexOf('node') !== -1 && process.argv.length > 2) { | |
channel = process.argv[2]; | |
} else if (process.argv[0].indexOf('delete') !== -1 && process.argv.length > 1) { | |
channel = process.argv[1]; | |
} else { | |
console.log('Usage: node ./delete-slack-messages.js CHANNEL_ID'); | |
process.exit(1); | |
} | |
const https = require('https'); | |
const baseApiUrl = 'https://slack.com/api/'; | |
const messages = []; | |
const historyApiUrl = baseApiUrl + 'conversations.history?token=' + token + '&count=1000&channel=' + channel + '&cursor='; | |
const deleteApiUrl = baseApiUrl + 'chat.delete?token=' + token + '&channel=' + channel + '&ts=' | |
let delay = 300; // Delay between delete operations in milliseconds | |
let nextCursor = ''; | |
// --------------------------------------------------------------------------------------------------------------------- | |
function deleteMessage() { | |
if (messages.length == 0) { | |
if (nextCursor) { | |
processHistory(); | |
} | |
return; | |
} | |
const ts = messages.shift(); | |
https.get(deleteApiUrl + ts, function (res) { | |
let body = ''; | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function(){ | |
const response = JSON.parse(body); | |
if (response.ok === true) { | |
console.log(ts + ' deleted!'); | |
} else if (response.ok === false) { | |
console.log(ts + ' could not be deleted! (' + response.error + ')'); | |
if (response.error === 'ratelimited') { | |
delay += 100; // If rate limited error caught then we need to increase delay. | |
messages.push(ts); | |
} | |
} | |
setTimeout(deleteMessage, delay); | |
}); | |
}).on('error', function (e) { | |
console.error("Got an error: ", e); | |
}); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
function processHistory() { | |
https.get(historyApiUrl + nextCursor, function(res) { | |
let body = ''; | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function () { | |
nextCursor = null; | |
const response = JSON.parse(body); | |
if (response.messages && response.messages.length > 0) { | |
if (response.has_more) { | |
nextCursor = response.response_metadata.next_cursor; | |
} | |
for (let i = 0; i < response.messages.length; i++) { | |
messages.push(response.messages[i].ts); | |
} | |
deleteMessage(); | |
} | |
}); | |
}).on('error', function (e) { | |
console.error("Got an error: ", e); | |
}); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
processHistory(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment