Skip to content

Instantly share code, notes, and snippets.

@waghcwb
Forked from firatkucuk/delete-slack-messages.js
Last active June 3, 2019 22:14
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 waghcwb/fb748d6bf3c5ec89ff2862f3e0284d55 to your computer and use it in GitHub Desktop.
Save waghcwb/fb748d6bf3c5ec89ff2862f3e0284d55 to your computer and use it in GitHub Desktop.
Deletes slack public/private channel and chat messages.
#!/usr/bin/env node
// Channel ID is on the the browser URL.: https://mycompany.slack.com/messages/MYCHANNELID/
// Pass it as a parameter: node ./delete-slack-messages.js CHANNEL_ID
// CONFIGURATION #######################################################################################################
const token = '<token>'; // You can learn 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.ok) {
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();
}
} else {
throw new Error(response.error)
}
});
}).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