Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This Ruby script will bulk remove all Slack files older than 30 days. Just add your API token from https://api.slack.com/web#authentication 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!'

I had some Ruby issues on my PC, so I rewrote this in Python: https://gist.github.com/jackcarter/d86808449f0d95060a40

Thanks James!

Owner

jamescmartinez commented Jan 14, 2016

No problem @jackcarter!

drew2a commented Mar 14, 2016

Thanks!

@jamescmartinez and @jackcarter Thanks! It remains a bit unclear whether this takes advantage of admin rights to delete all files for the team as opposed to only for the user (the latter feature is also made available through http://reallygood.work/slack without the potential hassle of having to run Ruby or Python).

jkbz commented Apr 4, 2016

@pimschaaf It looks like the script acts with whatever permissions are assigned to the token's user. If the user has admin privileges then the script can delete everything; if the user doesn't have admin privileges then it will only delete files associated with the user.

And by "looks like" I mean that's what it does. I tested it today :)

What about private files?

Worked =D Thanks bro

qiugits commented May 1, 2016

/Hi, I'm not a professional programmer and want to use this.
/I have a question: how can I get this 'uri' and 'net/http' library? Or should it already be installed?
/I'm using Mac and 2.0.0 ruby.

Oh it worked without installing anything. Thanks a lot!

dilzio commented May 11, 2016

worked like a charm...thanks! :)

qiugits commented May 27, 2016

Hi,
In slack, in the "files" section it shows the files are deleted, but in "statistics" section it's not. What could be the reason? I still get "no storage" pop-ups due to this...

GR4YSON commented May 27, 2016

This works like a charm! Thank you so much.

Hi, i got the same issue QiuqiuPro still after 2 days or so now.

To get this to run, I had to add this to the script:

require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

Otherwise I was getting SSL verification errors.

lyrixx commented Aug 2, 2016

And the PHP version

@qiuqiupro and @simonelberts, I'm having that same issue. When I list files via the API as an owner, I see 500 MB of files. But the statistics show >5 GB. I'm assuming these extra 1200 files are private between users, but as an owner of the team it seems there should be a way to delete them.

nickell commented Aug 10, 2016

@bryanjacquot Same issue here, any luck yet?

xarses commented Aug 30, 2016

@bryanjacquot @nickell @qiuqiupro @simonelberts
WRGT stats, the stats graphic, count doesn't update instantly, it appears to be ~10-15 min cached
WRGT file count, slack support has confirmed that is most likely private message shares which admins MAY NOT see. Support Also notes that Admins may not interact with these files and proposes that you ask users to self manage them (obviously I replied that that isn't OK, I'll update if I get anywhere with that)

This is awesome, thank you.

leoduquesnel commented Oct 21, 2016

It worked for me!

tgensol commented Nov 4, 2016

any news @xarses ?

We just created a new slack extension called flack (f_) that should run well https://delete-slack-files.com/ .
The free feature lets you delete files which is what is crucial.

Slack has a max_count_limit of 100 so you actually have to run this several times despite a count of 1000 provided.

thank you.

oleyb commented Mar 10, 2017

This somehow deleted ALL of our files, not just things older than 30 days.

How to fix this?
$ ruby slack_delete2.rb
"Deleting files..."
slack_delete2.rb:35:in <main>': undefined methodmap' for nil:NilClass (NoMethodError)

@vadimpeskov I was facing the same issue.

Problem was that I was trying to use the client secret of the slack application that I created from here: https://api.slack.com/web#authentication

But this is generating the "invalid_token" error and so the list_files method was returning nil value and for this reason the error "undefined methodmap' for nil:NilClass" was raised at line 35.

I solved this issue by generating a real token (NOT client_secret or cliend_id related to an app).
To generate a token you have to follow this guide: https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens

@jamescmartinez many thanks for this! But please consider to update the description of how to use this by telling people to generate the legacy token as reported here: https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens

@jamescmartinez Super helpful!

Ran into the same issue as @jwilkinson with the 100 files per run. For people with a ton of files, you can run it multiple times with:

for run in {1..10}
do
  ruby slack_delete.rb
done

Change the outside limit on the range to be as big as you need it to be.

I ran the script yesterday using the token generated following @diegodurante post and it seems to only have deleted my user files, I am a team owner, any ideas?

tomleo commented Jul 24, 2017

I just built a python script that does basically the same thing but will a few more command line arguments https://gitlab.com/tomleo/SlackFileCleanup/ I would have forked this repo but I don't know ruby. ☮️

AknEp commented Sep 29, 2017

Great script!

I updated Line 35 to this:

file_ids = files.select{ |f| f["size"] > 10000 }.map { |f| f['id'] }

It removes only heavy files (> 10KB).

icfantv commented Oct 28, 2017

OMG. ❤️ Thank you so much!

Hungor commented Dec 4, 2017

Thanks!

Very helpful. Also very easy to parse your code so I could figure out what you were doing before I ran it ;)

Antol commented Jan 17, 2018

Is it only me? I have successfully deleted a big bunch of files but Slack Analitycs still shows the same size of storage usage. I am sure that files have been deleted because if I go to Files panel I can see only 99 instead of few thousands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment