Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 = 'SLACK 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();
@ugurayan

This comment has been minimized.

ugurayan commented Jul 18, 2017

Nice code

@horstepipe

This comment has been minimized.

horstepipe commented Oct 6, 2017

would it be possible to remove DMs, too?

@woodyb23

This comment has been minimized.

woodyb23 commented Oct 7, 2017

Code deletes the messages but when I come back they are there again. Any ideas?

@gummi-io

This comment has been minimized.

gummi-io commented Nov 30, 2017

Thank you so much!! Exactly what i needed.

I modified the code a bit to delete direct messages and continue deleting after 1000 limit. If anyone is interested:
https://gist.github.com/gummi-io/f3dbfebfcd5fd1fc4e42da1c0e2b41c8 ;)

@mtruban

This comment has been minimized.

mtruban commented Jan 3, 2018

thanks man. works like a charm

@niiongi

This comment has been minimized.

niiongi commented Jan 26, 2018

Hi there! I'm a bit of a noob when it comes to APIs and in need of a bit of help..

Do I understand correctly that in order to wipe my message history in my #general, I: 1) modify the raw code; 2) DL it as a .zip file; 3) import it to my Slack?

If so, what are the fields I need to modify in the raw code and how can I find my #general channel ID?

TY

@aissaghouti

This comment has been minimized.

@djdenv

This comment has been minimized.

djdenv commented Mar 27, 2018

@woodyb23 (and others) Slack imposes an API request limit of 1 per second (https://api.slack.com/docs/rate-limits#web). Short bursts are allowed, but once you hit 20 messages or so, requests will start getting rejected. Modify the settings of this script to use a delay of say 1250ms so you won't hit the rate limit and messages will be deleted properly.

@yunabsd

This comment has been minimized.

yunabsd commented Mar 28, 2018

Thank you very much~!

I modified the code a bit, also added promise methods, delete all messages, and delete direct message. :)
https://github.com/yunabsd/SlackRemoveMe

@CraigReville

This comment has been minimized.

CraigReville commented May 24, 2018

Having issues when using it for 1-on-1 Chat, can't get around this:

/Users/name/Scripts/delete-channel-messages.js:66
        for (var i = 0; i < response.messages.length; i++) {
                                              ^

TypeError: Cannot read property 'length' of undefined
    at IncomingMessage.<anonymous> (/Users/name/Scripts/delete-channel-messages.js:66:47)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1091:14)
    at process._tickCallback (internal/process/next_tick.js:174:19)

BUMP**

Can't figure out why but it seems cannot delete chats, just general group chats.

EDIT:
Just read that the feature doesn't exist to delete private messages via api anymore, they allow you to delete view archive and that's only available on the paid plan.

Basically, they are forcing you to pay.

@firatkucuk

This comment has been minimized.

Owner

firatkucuk commented Jul 20, 2018

Now, It supports chat messages as well. And automatically proceed to the next 1000 messages.

@gijo-varghese

This comment has been minimized.

gijo-varghese commented Aug 1, 2018

Not working

@nedzen

This comment has been minimized.

nedzen commented Aug 8, 2018

super neat ! thank you !

@roelvdboom

This comment has been minimized.

roelvdboom commented Sep 6, 2018

Works perfectly!

@iLoreto

This comment has been minimized.

iLoreto commented Oct 27, 2018

Thank you for sharing. I had a dream where hubot had this functionality in coffee script!
/hubot cleanup --start=2017-1-1 --end=2018-1-1 (admins permissions only)

KUDOS!

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