Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created October 3, 2016 15:51
Show Gist options
  • Save yitsushi/34b99c6022fcfe64c361ccbf47d1f880 to your computer and use it in GitHub Desktop.
Save yitsushi/34b99c6022fcfe64c361ccbf47d1f880 to your computer and use it in GitHub Desktop.
delete old slack files (only images)
require "slack"
#
# gem install slack-api
#
# Get the token from here: https://api.slack.com/docs/oauth-test-tokens
#
client = Slack::Client.new token: ENV['TOKEN']
LIMIT = 20 # days
user = client.auth_test
puts "#{user['user']} (#{user['user_id']})"
from_time = Time.now.to_i - (60 * 60 * 24 * LIMIT)
pages = client.files_list(user: user['user_id'], types: 'images', ts_to: from_time)['paging']['pages'].to_i
deleted_files_count = 0
cant_delete = 0
while pages > 0 do
files = client.files_list(user: user['user_id'], page: pages, ts_to: from_time, types: 'images')['files']
files.each_with_index do |file, index|
print sprintf('Page %-4d => %10s (%d/%d)', pages, file['id'], index, files.length)
response = client.files_delete(file: file['id'])
deleted_files_count += 1 if response['ok']
cant_delete += 1 if response['error'] == 'cant_delete_file'
print "\r"
end
pages -= 1
end
puts sprintf('%-80s', "Deleted files: #{deleted_files_count}")
puts sprintf('%-80s', "Permission denied: #{cant_delete}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment