-
-
Save jamescmartinez/909401b19c0f779fc9c1 to your computer and use it in GitHub Desktop.
require 'net/http' | |
require 'json' | |
require 'uri' | |
@token = '' | |
def list_files | |
ts_to = (Time.now - 30 * 24 * 60 * 60).to_i # 30 days ago | |
params = { | |
token: @token, | |
ts_to: ts_to, | |
count: 1000 | |
} | |
uri = URI.parse('https://slack.com/api/files.list') | |
uri.query = URI.encode_www_form(params) | |
response = Net::HTTP.get_response(uri) | |
JSON.parse(response.body)['files'] | |
end | |
def delete_files(file_ids) | |
file_ids.each do |file_id| | |
params = { | |
token: @token, | |
file: file_id | |
} | |
uri = URI.parse('https://slack.com/api/files.delete') | |
uri.query = URI.encode_www_form(params) | |
response = Net::HTTP.get_response(uri) | |
p "#{file_id}: #{JSON.parse(response.body)['ok']}" | |
end | |
end | |
p 'Deleting files...' | |
files = list_files | |
file_ids = files.map { |f| f['id'] } | |
delete_files(file_ids) | |
p 'Done!' |
I have a trouble... When I running the script, I get a lot of messages like this
"F206K98J3: false"
All files after the script is finished remain not deleted
UPD: Resolved - you must be an admin of your workspace to delete files
Anyone modify it to download the file before deleting it? Would be useful, so you can free Slack storage but still have some stuff archived.
@MikeLund, it only seems to work with current year files, anything older remains.
this is great, thanks! I converted to nodejs to better fit my environment. check it out here https://gist.github.com/evangs/85f70573a9046990df4ff820151e0b08
If you get rate limited by slack insert a wait on line 22
sleep 1
Found I had to go all the way down to one a second to avoid getting throttled.
I was using the script and worked like a charm, however last week it starting failing and I've no idea of Ruby, maybe I've got Ruby updated or something? It throws away this error:
bulk_slack_files.rb:35:in <main>': undefined method
map' for nil:NilClass (NoMethodError)
line 35 is the same as the one shown in the gist file_ids = files.map { |f| f['id'] }
Any clue? Thanks!
@nandodelauni I had the same error and it was because I was using an invalid token
thanks @luisenrike!
here's the new URL for generating Legacy workspace tokens:
https://api.slack.com/custom-integrations/legacy-tokens
Sorry, I'm not a programmer and am having problems figuring out where to run this code.
Edit: Ahh nevermind, a colleague helped me out. I had to download node.js first :-(
I just added a node gist to batch delete files older than a year
https://gist.github.com/dominikwilkowski/76d99ae84848c39e54494f4dbdd4327d
Comes with instructions too :)
An easier way is to use, no coding required.
I get an HTML doc with little helpful error handling, that starts:
400 ERROR
The request could not be satisfied.
@ayhamg Right now that link is 404.
I just developed a bot that helps you to manage this, any contribution is welcome!
https://github.com/danibram/scrapy-slack-bot
Great script, thanks