Created
March 10, 2017 14:53
-
-
Save r4m/775a1556ff0d5d53e5824f5698e56cb5 to your computer and use it in GitHub Desktop.
Script for quickly delete files from Slack
This file contains 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
# Script to delete slack files starting from 7 days ago | |
require 'net/http' | |
require 'json' | |
api_key = "<YOUR-API-KEY-GOES-HERE>" | |
day_ago = 1488551417 # 7. Otherwise 30: 1486565133 | |
user_url = "https://slack.com/api/users.list?token=#{api_key}" | |
uri = URI(user_url) | |
response = Net::HTTP.get(uri) | |
member_ids = JSON.parse(response)["members"].map {|res| res["id"]} | |
member_ids.each do |member_id| | |
page = 1 | |
pages = 2 | |
while page < pages | |
puts "Delete files for user #{member_id} at page #{page}" | |
file_url = "https://slack.com/api/files.list?token=#{api_key}&ts_to=#{day_ago}&user=#{member_id}&page=#{page}" | |
uri = URI(file_url) | |
response = JSON.parse(Net::HTTP.get(uri)) | |
if response["ok"] | |
page = response["paging"]["page"] | |
pages = response["paging"]["pages"] | |
file_ids = response["files"].map {|res| res["id"]} | |
file_ids.each do |file_id| | |
puts "Delete file #{file_id}" | |
delete_url = "https://slack.com/api/files.delete?file=#{file_id}&token=#{api_key}" | |
uri = URI(delete_url) | |
response = Net::HTTP.get(uri) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment