Last active
November 29, 2023 07:03
-
-
Save jackcarter/d86808449f0d95060a40 to your computer and use it in GitHub Desktop.
Delete Slack files older than 30 days. Rewrite of https://gist.github.com/jamescmartinez/909401b19c0f779fc9c1
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 requests | |
import time | |
import json | |
token = '' | |
#Delete files older than this: | |
ts_to = int(time.time()) - 30 * 24 * 60 * 60 | |
def list_files(): | |
params = { | |
'token': token | |
,'ts_to': ts_to | |
,'count': 1000 | |
} | |
uri = 'https://slack.com/api/files.list' | |
response = requests.get(uri, params=params) | |
return json.loads(response.text)['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 = requests.get(uri, params=params) | |
print count, "of", num_files, "-", file_id, json.loads(response.text)['ok'] | |
files = list_files() | |
file_ids = [f['id'] for f in files] | |
delete_files(file_ids) |
@IlanVivanco good job! saved my time.
None of the scripts listed here works anymore for me. Something changed @slack?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of today, legacy tester tokens may no longer be created.
However, you can still use this script following this steps:
Create a Slack app on https://api.slack.com/apps?new_app=1 and assign that to your workspace.
Then within this new app, go to OAuth & Permissions and in the section Scopes > User Token Scopes add the
files:read
andfiles: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.
I have also adapted the code to work with the Oauth authentication.
https://gist.github.com/IlanVivanco/d2a96abb364ccb3b59e198f1c5fdf673