Today I had the challenge of wiping a Slack workspace/team of all the chat history. There isn’t a handy one click button to do this. The slow way around this is to go through each channel one by one, deleting the channel and then recreating it. This works with everything until you get to the
#general channel (or whatever it was set up to be).
There are a couple of Chrome extensions which promise to go through a given channel and delete each message one by one. They either don’t work, or would have charged me money to (probably) not work. They might have worked a while ago, however Slack has changed, updated APIs, etc, and now they are dead.
Further reading of the internet led me a solution which involved a very small amount of code (Python) and toying with the Slack API.
⚠️ Being a tech post relying on a third party API and code tinkering, give it a couple of
years weeks and it’ll be out of date like those Chrome extensions. The year of writing is 2021.
Slack Cleaner 2 or slack_cleaner2 is the library of choice for this exercise. As with most things in the Python world you can install it via pip (
pip3 install slack_cleaner2), or pull it straight into a Docker container if that’s your thing.
Slack Cleaner 2 is a wrapper around the Slack API, so you’ll need an API key. In Slack API tools you’ll need to:
The API access is all setup, and the permissions are granted. In a text editor you can copy-and-pasta the following python code into a new file called
cleaner.py, replacing the OAuth token, and channel name as appropriate:
from slack_cleaner2 import SlackCleaner
s = SlackCleaner("PUT-YOUR-USER-OAUTH-TOKEN-HERE", sleep_for=1)
for msg in s.c['replace-with-channel-name'].msgs(with_replies=True):
👆 For each message in the channel name (including message threads)… delete it.
Save that file somewhere sensible… like your desktop… (why not!) And then run it with:
That’s assuming that you saved it to the desktop… But you’re smart right?
It’ll take it’s time, just deleting a message each second. If you have a particularly large channel history it may timeout, but you can run the same command again and it’ll start again where it left off.
Other links that may be helpful: