Skip to content

Instantly share code, notes, and snippets.

@DarthJahus
Created March 5, 2019 22:06
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 DarthJahus/dfe8f0c9989e46b8c625838cd99a96fd to your computer and use it in GitHub Desktop.
Save DarthJahus/dfe8f0c9989e46b8c625838cd99a96fd to your computer and use it in GitHub Desktop.
NodeJS script to delete messages in a Slack channel.
#!/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 = ''; // 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.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();
@DarthJahus
Copy link
Author

DarthJahus commented Mar 5, 2019

Credits to someone on the Internet.

Edit:

Original script by @firatkucuk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment