Clearing old Slack messages

Jerry M.
2 min readJun 14, 2021

Unless you are an enterprise customer of Slack, you can’t write a program to clear old Slack messages.

Or can you? I set out to find out how to bypass this limitation.

First, I tried clearing the old messages by using a bot with proper permissions. Unfortunately, that’s when I found out that you have to use the User OAuth Token of the bot and that this feature is only available for enterprise users. Too bad!.

Slack allows you to run the messenger client from the browser. The browser version of the Slack client is available under app.slack.com. This is good because there would be an API most of the time, and we can use it. Too bad if Slack is using server-side rendering. Luckily, it was not.

Looking at the browser developer tools confirmed that Slack is using APIs. Good news!

Permissions

Before we go further, I must mention that I’m a Slack admin. Being an admin allows me to delete any message from any group (unless it is private, I think) without joining the group first.

If you are not a Slack admin, then this won’t work for you.

The APIs

By fetching the messages and deleting them on the UI, I found the two APIs to get the job done.

You have to send a form-data request via POST on both these APIs and a cookie header for authentication.

Why is Slack using the HTTP POST verb for delete, I would never know. But their code, their app, their rules!

We now have enough to automate the whole process. I wrote the code in nodejs with axios and progress. Head over to https://github.com/jerrymannel/slackMessageCleaner for the complete working code.

--

--

Jerry M.

Manages technology @ appveen. Technology enthusiast. Admirer of algorithms. Husband, dad and beer lover. Views are my own or could have been coerced by my wife.