Skip to content

Instantly share code, notes, and snippets.

@diatche
Created September 2, 2020 05:40
Show Gist options
  • Save diatche/ca905ed7d22c753a8d474e1ebf05fc29 to your computer and use it in GitHub Desktop.
Save diatche/ca905ed7d22c753a8d474e1ebf05fc29 to your computer and use it in GitHub Desktop.
Deletes old bot Slack messages using slack_cleaner2
# Deletes old bot Slack messages using slack_cleaner2
# https://github.com/sgratzl/slack-cleaner
# Usage:
# 1. Install slack_cleaner2 and Arrow
# 2. Run with token as CLI arg. E.g. `python slack_cleaner.py <token xoxp-...>`
import arrow
import time
import sys
from slack_cleaner2 import *
date_max = arrow.now().shift(days=-7).floor('day')
date_format = 'DD/MM/YYYY'
token = str(sys.argv[1])
s = SlackCleaner(token)
# list of all kind of channels
convs = s.conversations
# delete all messages in -bot channels
channels = list(filter(match('.*-bot'), convs))
print(f'Deleting bot messages before {date_max.format(date_format)} from channels: {channels}')
messages = s.msgs(
channels=channels,
before=date_max.timestamp
)
for msg in messages:
if msg.bot:
# date = arrow.get(msg.ts).to('local')
# print(f'Deleting {msg.user or "bot"} message dated {date}')
msg.delete()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment