Skip to content

Instantly share code, notes, and snippets.

@sam-k3nny
Forked from Paradoxis/slack_delete.py
Created July 17, 2018 02:29
Show Gist options
  • Save sam-k3nny/9774a087f3a49ba9ee5ec2c22b6b2519 to your computer and use it in GitHub Desktop.
Save sam-k3nny/9774a087f3a49ba9ee5ec2c22b6b2519 to your computer and use it in GitHub Desktop.
Delete all Slack files. Usage: python slack_delete.py --token <your token>
import argparse
import requests
import time
import json
SLEEP = 3
def main():
"""
Entry point of the application
:return: void
"""
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--token", required=True, help="Specifies the OAuth token used for authentication, created at (https://api.slack.com/docs/oauth-test-tokens)")
parser.add_argument("-d", "--days", type=int, default=None, help="Delete files older than x days (optional)")
parser.add_argument("-c", "--count", type=int, default=1000, help="Max amount of files to delete at once (optional)")
parser.add_argument("--dryrun", help="Test the command by doing a dry run", action='store_true')
options = parser.parse_args()
print options
try:
print "[*] Fetching file list.."
file_ids = list_file_ids(token=options.token, count=options.count, days=options.days)
if options.dryrun:
print "[*] DRY RUN. The above {} files would be deleted.".format(len(file_ids))
else:
print "[*] Deleting files.."
delete_files(token=options.token, file_ids=file_ids)
print "[*] Done"
except KeyboardInterrupt:
print "\b\b[-] Aborted"
exit(1)
def calculate_days(days):
"""
Calculate days to unix time
:param days: int
:return: int
"""
return int(time.time()) - days * 24 * 60 * 60
def list_file_ids(token, count, days=None):
"""
Get a list of all file id's
:param token: string
:param count: int
:param days: int
:return: list
"""
if days:
params = {'token': token, 'count': count, 'ts_to': calculate_days(days)}
else:
params = {'token': token, 'count': count}
uri = 'https://slack.com/api/files.list'
response = requests.get(uri, params=params)
files = json.loads(response.text)['files']
for f in files:
#print f['permalink']
print json.dumps(f, indent=1)
largest_file = sorted(files, key = lambda f: f['size'], reverse=True)[0]
print 'largest file: ', largest_file['permalink'], largest_file['size']
return [f['id'] for f in files]
def delete_files(token, file_ids):
"""
Delete a list of files by id
:param token: string
:param file_ids: list
:return: void
"""
count = 0
num_files = len(file_ids)
for file_id in file_ids:
count += 1
params = {'token': token, 'file': file_id}
uri = 'https://slack.com/api/files.delete'
response = json.loads(requests.get(uri, params=params).text)
if response["ok"]:
print "[+] Deleted", count, "of", num_files, "-", file_id
else:
print "[!] Unable to delete", count, "of", num_files, "-", file_id + ", reason:", response["error"]
print "[!] Sleeping for {} seconds...".format(SLEEP)
time.sleep(SLEEP)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment