Last active
August 30, 2018 03:39
-
-
Save mattstabeler/3b563f328d97c535693ee4d28caea0a7 to your computer and use it in GitHub Desktop.
Python script to delete your old files from slack
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
#!/usr/local/bin/python | |
import urllib, json, time | |
# find files older than X and delete them | |
# https://api.slack.com/docs/oauth-test-tokens | |
token = "your-token-here" | |
# https://api.slack.com/methods/users.identity looks like "U2XXXXXXX" | |
user = "your-user-id" | |
def main(): | |
olderthan = 15 | |
numberoffiles = 100 | |
print "Deleting up to {} files older than {} days".format(numberoffiles, olderthan) | |
files = file_list(olderthan, numberoffiles) | |
for file in files['files']: | |
resp = delete_file(file['id']) | |
if resp['ok']: | |
print "Deleted {} : {}".format(file['id'], file['permalink']) | |
elif resp['error']: | |
print "ERROR: {} when deleting: {} : {}".format(resp['error'], file['id'], file['permalink']) | |
def file_list(daysago=100, filecount=1): | |
ts = long(time.time()) - (60 * 60 * daysago) | |
url = "https://slack.com/api/files.list?token={}&user={}&ts_to={}&count={}".format(token,user, ts, filecount) | |
return call(url) | |
def delete_file(file_id): | |
url = "https://slack.com/api/files.delete?token={}&file={}".format(token, file_id) | |
return call(url) | |
def call(url): | |
response = urllib.urlopen(url) | |
data = json.loads(response.read()) | |
return data | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment