Skip to content

Instantly share code, notes, and snippets.

@diegodurante
Forked from jamescmartinez/slack_delete.rb
Last active April 10, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegodurante/542c2b081fc08c79b02728ccb6fe8254 to your computer and use it in GitHub Desktop.
Save diegodurante/542c2b081fc08c79b02728ccb6fe8254 to your computer and use it in GitHub Desktop.
This Ruby script will bulk remove all Slack files older than 30 days. Create your legacy token form here: https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens and then add it into the token quotes at the top of the file.
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!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment