Skip to content

Instantly share code, notes, and snippets.

@martinboy
Last active July 14, 2016 12:28
Show Gist options
  • Save martinboy/a8fa65e9deba8d4cf0b0194c59e758de to your computer and use it in GitHub Desktop.
Save martinboy/a8fa65e9deba8d4cf0b0194c59e758de to your computer and use it in GitHub Desktop.
Bulk remove files from Slack for the period
#
# source https://www.shiftedup.com/2014/11/13/how-to-bulk-remove-files-from-slack
#
import requests
import json
import calendar
from datetime import datetime, timedelta
### Input vars ###
# OAuth test token https://api.slack.com/docs/oauth-test-tokens
_token = "YOUR TOKEN GOES HERE"
# Team's Slack domain
_domain = "YOUR SUB DOMAIN HERE"
# Interval (in days) to delete
_days = -30
if __name__ == '__main__':
while 1:
files_list_url = 'https://slack.com/api/files.list'
date = str(calendar.timegm((datetime.now() + timedelta(-30))
.utctimetuple()))
data = {"token": _token, "ts_to": date}
response = requests.post(files_list_url, data = data)
if len(response.json()["files"]) == 0:
break
for f in response.json()["files"]:
print "Deleting file " + f["name"] + "(" + f["id"] + ")" + "..."
timestamp = str(calendar.timegm(datetime.now().utctimetuple()))
delete_url = "https://" + _domain + ".slack.com/api/files.delete?t=" + timestamp
r = requests.post(delete_url, data = {
"token": _token,
"file": f["id"],
"set_active": "true",
"_attempts": "1"})
if (not r.json()["ok"]):
print "Error: " + r.json()["error"]
else:
print "OK"
print "DONE!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment