Skip to content

Instantly share code, notes, and snippets.

@IlanVivanco
Last active January 27, 2022 08:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save IlanVivanco/d2a96abb364ccb3b59e198f1c5fdf673 to your computer and use it in GitHub Desktop.
Save IlanVivanco/d2a96abb364ccb3b59e198f1c5fdf673 to your computer and use it in GitHub Desktop.
Deletes files older than N days. You can also filter them by size. Forked from https://gist.github.com/jackcarter/d86808449f0d95060a40
# Idea taken from https://gist.github.com/jackcarter/d86808449f0d95060a40
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'
# 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
}
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()['ok'])
)
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