-
-
Save zernel/6e0156606b555537d6335e5c55b2a396 to your computer and use it in GitHub Desktop.
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
# Idea taken from https://gist.github.com/jackcarter/d86808449f0d95060a40 | |
# This script requires python lib: requests | |
# You can install by: `python -m pip install requests` | |
import time | |
import codecs | |
import requests | |
reader = codecs.getreader("utf-8") | |
# Obtain your's by creating a Slack app on https://api.slack.com/apps?new_app=1 and assign that to your workspace. | |
# Within this new app, go to "OAuth & Permissions" and in the section "Scopes > User Token Scopes" add the "files:read" and "files:write" Oauth scopes. | |
# Finally, you can now go to the top of the page and install the app to the workspace. After that you'll get the User OAuth Token that you can use on the script. | |
# Bare in mind that if you forget to add the scopes, or you need to modify them after you install the app to the workspace, you would need to reinstall it after changing the scopes. | |
token = 'xoxp-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
# Delete files older than this: | |
days = 30 | |
ts_to = int(time.time()) - days * 24 * 60 * 60 | |
# How many files? (Maximum is 1000, otherwise it defaults to 100). | |
count = 1000 | |
# Which types? | |
# types = 'all' | |
# types = 'spaces,snippets,images,gdocs,zips,pdfs' | |
types = 'images,zips,pdfs' | |
# Filter the files created by yourself. | |
# How to get your Member ID? | |
# 1. Click on a user name within Slack. | |
# 2. Click on "View profile" in the menu that appears. | |
# 3. Click the more icon "..." | |
# 4. Click on "Copy Member ID." | |
user_id = 'UXXXXXXXX' | |
# More info here: https://api.slack.com/methods/files.list | |
def list_files(): | |
uri = 'https://slack.com/api/files.list' | |
header = {'Authorization': 'Bearer ' + token} | |
data = { | |
'ts_to': ts_to, | |
'count': count, | |
'types': types, | |
'user': user_id | |
} | |
response = requests.post(uri, data=data, headers=header) | |
return response.json() | |
def filter_by_size(files, mb, greater_or_smaller): | |
if greater_or_smaller == 'greater': | |
return [file for file in files if (file['size'] / 1000000) > mb] | |
elif greater_or_smaller == 'smaller': | |
return [file for file in files if (file['size'] / 1000000) < mb] | |
else: | |
return None | |
def delete_files(files): | |
num_files = len(files) | |
uri = 'https://slack.com/api/files.delete' | |
header = {'Authorization': 'Bearer ' + token} | |
for index, file in enumerate(files): | |
data = {'file': file['id']} | |
response = requests.post(uri, data=data, headers=header) | |
print( | |
str(index + 1) + " of " + str(num_files) + ' |', | |
'ID: ' + file['id'] + ' |', | |
'File: ' + file['name'] + ' |', | |
'Response: ' + str(response.json()) | |
) | |
slack_data = list_files() | |
files = slack_data['files'] | |
# You can filter the files by size before passing them to the delete function. | |
# Uncomment the next line to enable the filter. e.g.: Only files bigger than 1MB. | |
# files = filter_by_size(files, 1, 'greater') | |
# Files found. | |
print(str(len(files)) + ' files found.') | |
# Uncomment the next line to delete files. | |
delete_files(files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work for me anymore. Did something change @slack?