Created
June 6, 2016 22:16
-
-
Save dyndna/fba8a7058a8b2650edd36aff8b1566e2 to your computer and use it in GitHub Desktop.
Slack delete files older than n days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import urllib2 | |
import time | |
import json | |
#Credit: https://gist.github.com/jackcarter/d86808449f0d95060a40 and | |
# https://gist.github.com/jackcarter/d86808449f0d95060a40#gistcomment-1755139 | |
# put oauth test token between '' | |
token = '' | |
#Delete files older than this: | |
days = 30 | |
ts_to = int(time.time()) - days * 24 * 60 * 60 | |
def list_files(): | |
params = { | |
'token': token, | |
'ts_to': ts_to, | |
'count': 1000, | |
} | |
uri = 'https://slack.com/api/files.list' | |
response = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read() | |
return json.loads(response)['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 = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read() | |
print count, "of", num_files, "-", file_id, json.loads(response)['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