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

后端 未结 11 546
暖寄归人
暖寄归人 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:40

    I wrote a simple node script for deleting messages from public/private channels and chats. You can modify and use it.

    https://gist.github.com/firatkucuk/ee898bc919021da621689f5e47e7abac

    First, modify your token in the scripts configuration section then run the script:

    node ./delete-slack-messages CHANNEL_ID
    

    Get an OAuth token:

    1. Go to https://api.slack.com/apps
    2. Click 'Create New App', and name your (temporary) app.
    3. In the side nav, go to 'Oauth & Permissions'
    4. On that page, find the 'Scopes' section. Click 'Add an OAuth Scope' and add 'channels:history' and 'chat:write'. (see scopes)
    5. At the top of the page, Click 'Install App to Workspace'. Confirm, and on page reload, copy the OAuth Access Token.

    Find the channel ID

    Also, the channel ID can be seen in the browser URL when you open slack in the browser. e.g.

    https://mycompany.slack.com/messages/MY_CHANNEL_ID/
    

    or

    https://app.slack.com/client/WORKSPACE_ID/MY_CHANNEL_ID
    
    0 讨论(0)
  • 2021-01-30 06:42

    If you like Python and have obtained a legacy API token from the slack api, you can delete all private messages you sent to a user with the following:

    import requests
    import sys
    import time
    from json import loads
    
    # config - replace the bit between quotes with your "token"
    token = 'xoxp-854385385283-5438342854238520-513620305190-505dbc3e1c83b6729e198b52f128ad69'
    
    # replace 'Carl' with name of the person you were messaging
    dm_name = 'Carl'
    
    # helper methods
    api = 'https://slack.com/api/'
    suffix = 'token={0}&pretty=1'.format(token)
    
    def fetch(route, args=''):
      '''Make a GET request for data at `url` and return formatted JSON'''
      url = api + route + '?' + suffix + '&' + args
      return loads(requests.get(url).text)
    
    # find the user whose dm messages should be removed
    target_user = [i for i in fetch('users.list')['members'] if dm_name in i['real_name']]
    if not target_user:
      print(' ! your target user could not be found')
      sys.exit()
    
    # find the channel with messages to the target user
    channel = [i for i in fetch('im.list')['ims'] if i['user'] == target_user[0]['id']]
    if not channel:
      print(' ! your target channel could not be found')
      sys.exit()
    
    # fetch and delete all messages
    print(' * querying for channel', channel[0]['id'], 'with target user', target_user[0]['id'])
    args = 'channel=' + channel[0]['id'] + '&limit=100'
    result = fetch('conversations.history', args=args)
    messages = result['messages']
    print(' * has more:', result['has_more'], result.get('response_metadata', {}).get('next_cursor', ''))
    while result['has_more']:
      cursor = result['response_metadata']['next_cursor']
      result = fetch('conversations.history', args=args + '&cursor=' + cursor)
      messages += result['messages']
      print(' * next page has more:', result['has_more'])
    
    for idx, i in enumerate(messages):
      # tier 3 method rate limit: https://api.slack.com/methods/chat.delete
      # all rate limits: https://api.slack.com/docs/rate-limits#tiers
      time.sleep(1.05)
      result = fetch('chat.delete', args='channel={0}&ts={1}'.format(channel[0]['id'], i['ts']))
      print(' * deleted', idx+1, 'of', len(messages), 'messages', i['text'])
      if result.get('error', '') == 'ratelimited':
        print('\n ! sorry there have been too many requests. Please wait a little bit and try again.')
        sys.exit()
    
    0 讨论(0)
  • 2021-01-30 06:43

    There is a slack tool to delete all slack messages on your workspace. Check it out: https://www.messagebender.com

    0 讨论(0)
  • 2021-01-30 06:44

    For anyone else who doesn't need to do it programmatic, here's a quick way:

    (probably for paid users only)

    1. Open the channel in web or the desktop app, and click the cog (top right).
    2. Choose "Additional options..." to bring up the archival menu. notes
    3. Select "Set the channel message retention policy".
    4. Set "Retain all messages for a specific number of days".
    5. All messages older than this time are deleted permanently!

    I usually set this option to "1 day" to leave the channel with some context, then I go back into the above settings, and set it's retention policy back to "default" to go continue storing them from now-on.

    Notes:
    Luke points out: If the option is hidden: you have to go to global workspace Admin settings, Message Retention & Deletion, and check "Let workspace members override these settings"

    0 讨论(0)
  • 2021-01-30 06:47

    default clean command did not work for me giving following error:

    $ slack-cleaner --token=<TOKEN> --message --channel <CHANNEL>
    
    Running slack-cleaner v0.2.4
    Channel, direct message or private group not found
    

    but following worked without any issue to clean the bot messages

    slack-cleaner --token <TOKEN> --message --group <CHANNEL> --bot --perform --rate 1 
    

    or

    slack-cleaner --token <TOKEN> --message --group <CHANNEL> --user "*" --perform --rate 1 
    

    to clean all the messages.

    I use rate-limit of 1 second to avoid HTTP 429 Too Many Requests error because of slack api rate limit. In both cases, channel name was supplied without # sign

    0 讨论(0)
  • 2021-01-30 06:49

    I quickly found out there's someone already made a helper: slack-cleaner for this.

    And for me it's just:

    slack-cleaner --token=<TOKEN> --message --channel jenkins --user "*" --perform

    0 讨论(0)
提交回复
热议问题