Slack clean all messages (~8K) in a channel

后端 未结 11 559
暖寄归人
暖寄归人 2021-01-30 06:00

We currently have a Slack channel with ~8K messages all comes from Jenkins integration. Is there any programmatic way to delete all messages from that channel? The web interface

11条回答
  •  梦谈多话
    2021-01-30 06:51

    As other answers allude, Slack's rate limits make this tricky - the rate limit is relatively low for their chat.delete API at ~50 requests per minute.

    The best strategy that respects the rate limit is to retrieve messages from the channel you want to clear, then delete the messages in batches under 50 that run on a minutely interval.

    I've built a project containing an example of this batching that you can easily fork and deploy on Autocode - it lets you clear a channel via slash command (and allows you restrict access to the command to just certain users of course!). When you run /cmd clear in a channel, it marks that channel for clearing and runs the following code every minute until it deletes all the messages in the channel:

    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
        };
      }
    });
    

    You can view the full code and a guide to deploying your own version here: https://autocode.com/src/jacoblee/slack-clear-messages/

提交回复
热议问题