Clear All Slack Channel Messages
This app clears all messages from a Slack channel with a simple slash command.
Once invoked, it will delete 40 messages per minute from the channel it is
invoked in until the channel has no messages left or the command is cancelled
by running /cmd clear cancel
.
For security, you can restrict who can trigger this command by setting
an variable with authorized Slack user ids.
Deployment is simple - all you need to do is press the install button to get started!
Note: You must have sufficient permissions in your Slack workspace to clear
messages posted by an admin or owner.
How It Works
This Source contains two endpoints:
functions/events/slack/command/clear.js
, which responds to an incoming
Slack command (invoked as /cmd clear
) and marks a Slack channel for clearing.
functions/events/scheduler/minutely.js
, which runs every minute using the
Autocode scheduler and deletes up to 40 messages using the
slack.messages.destroy API.
Slack has a rate limit of around 50 requests per minute around the message
deletion API, and running the deletion process minutely and deleting messages in
batches allows your app to respect this limit.
When you run /cmd clear
, your app will store the channel_id
and user_id
from the incoming slack.command
event using the
utils.kv API and update you with an
ephemeral message created with the
slack.messages.ephemeral API:
await lib.utils.kv['@0.1.16'].set({
key: `__clearedSlackChannelData:${context.service.identifier}__`,
value: `${event.channel_id},${event.user_id}`
});
return lib.slack.messages['@0.6.1'].ephemeral.create({
channelId: event.channel_id,
userId: event.user_id,
text: `Clearing channel <#${event.channel_id}>...`,
attachments: [{
text: `This app will run once a minute until it has deleted all the messages it can`
}, {
text: `You can run */cmd clear cancel* to stop this process`
}]
});
Once these keys are set, the Autocode scheduler will trigger minutely.js
every
minute, first retrieving a list of all messages in the channel with the
slack.channels.messages.list API:
let messages = await lib.slack.channels['@0.7.2'].messages.list({
channel: '#' + channel.name
});
And then clearing the messages from the channel, keeping track of which failures
are retryable:
console.log(`About to clear ${messages.length} messages from #${channel.name}...`);
let deletionResults = await async.mapLimit(messages, 2, async (message) => {
try {
await lib.slack.messages['@0.6.1'].destroy({
id: clearedChannelId,
ts: message.ts,
as_user: true
});
return {
successful: true
};
} catch (e) {
return {
successful: false,
retryable: e.message && e.message.indexOf('ratelimited') !== -1
};
}
});
Once no more messages remain in the channel or all failures are not retryable,
the app clears the key and sends a final ephemeral message.
You can preemptively stop the clearing process by running /cmd clear cancel
.
This will clear the values stored in
utils.kv and stop the scheduled endpoint
handler from deleting further messages in the channel.
Restricting Access
If you'd like to limit access to your command to certain users, open
functions/events/slack/command/clear.js
in the Autocode editor. This file contains
the code that runs in response to the slash command. Add the Slack user ids of
users you would like to allow to the SLACK_USER_ID_ALLOWLIST
array on line 3.
You can modify the entries on this list by changing the code and redeploying
your app.
Note: If you do not add any user ids here, this command will be usable by
anyone in your workspace. You can find the ids of users in your workspace by
viewing their profile and pressing the More button in the pane, then
retrieving the member ID in the menu that appears.
That's it!
Thanks for checking out this Source! Follow the Autocode team on Twitter
@AutocodeHQ for updates. If you have any
questions, please join our community Slack channel - you can get an invite from
under the community tab in the topbar of this page.