Skip to content

Instantly share code, notes, and snippets.

@seanlane
Forked from jackcarter/slack_delete.py
Last active February 8, 2019 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seanlane/09aa9ad3fdbb1668d2290036cd2735d7 to your computer and use it in GitHub Desktop.
Save seanlane/09aa9ad3fdbb1668d2290036cd2735d7 to your computer and use it in GitHub Desktop.
Delete Slack files older than 30 days. Rewrite of https://gist.github.com/jamescmartinez/909401b19c0f779fc9c1
#!/usr/bin/env python3
import requests
import time
import json
token = ''
#Delete files older than this:
days = 15
ts_to = int(time.time()) - days * 24 * 60 * 60 # Older than 7 days
def list_files():
params = {
'token': token
,'ts_to': ts_to
,'count': 1000
}
uri = 'https://slack.com/api/files.list'
response = requests.get(uri, params=params)
return json.loads(response.text)['files']
def delete_files(file_ids):
count = 0
num_files = len(file_ids)
for file_id in file_ids:
count = count + 1
params = {
'token': token
,'file': file_id
}
uri = 'https://slack.com/api/files.delete'
response = requests.get(uri, params=params)
print (count, "of", num_files, "-", file_id, json.loads(response.text)['ok'])
files = list_files()
file_ids = [f['id'] for f in files]
delete_files(file_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment