Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to delete old files on slack
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'uri'
@token = ''
def list_files(user = nil)
ts_to = (Time.now - 30 * 24 * 60 * 60).to_i # 30 days ago
params = {
token: @token,
ts_to: ts_to,
count: 1000
}
params[:user] = user['id'] if user
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 user_list
params = {
token: @token
}
uri = URI.parse('https://slack.com/api/users.list')
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
JSON.parse(response.body)['members']
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
puts 'Enter your slack username to delete user files (empty to exit)'
username = gets.chomp
exit if username.empty?
user = user_list.detect { |u| u['profile']['display_name'].eql?(username) }
files = if user
list_files(user)
else
list_files
end
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
You can’t perform that action at this time.