Last active
October 5, 2017 10:35
-
-
Save valer-cara/7876ce32d477564b3649cbca4680a790 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'json' | |
require 'uri' | |
# Get token at https://api.slack.com/custom-integrations/legacy-tokens | |
@token = '' | |
def me | |
@me ||= ( | |
params = { | |
token: @token, | |
} | |
uri = URI.parse('https://slack.com/api/auth.test') | |
uri.query = URI.encode_www_form(params) | |
response = Net::HTTP.get_response(uri) | |
JSON.parse(response.body) | |
) | |
end | |
def list_files(only_mine: true) | |
ts_to = (Time.now - 30 * 24 * 60 * 60).to_i # 30 days ago | |
params = { | |
token: @token, | |
ts_to: ts_to, | |
count: 1000 | |
} | |
params['user'] = me['user_id'] if only_mine | |
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 | |
files = list_files | |
p 'Confirm or CTRL-C outta here...' | |
files.each { |file| puts "- " + file['name'] } | |
gets | |
p 'Deleting 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