Skip to content

Instantly share code, notes, and snippets.

@jamescmartinez
Last active March 14, 2025 19:45
Show Gist options
  • Select an option

  • Save jamescmartinez/909401b19c0f779fc9c1 to your computer and use it in GitHub Desktop.

Select an option

Save jamescmartinez/909401b19c0f779fc9c1 to your computer and use it in GitHub Desktop.
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!'
@diegodurante

Copy link
Copy Markdown

@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

@kenshin6

Copy link
Copy Markdown

@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.

@theapplepastor

Copy link
Copy Markdown

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

tomleo commented Jul 24, 2017

Copy link
Copy Markdown

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

AknEp commented Sep 29, 2017

Copy link
Copy Markdown

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

icfantv commented Oct 28, 2017

Copy link
Copy Markdown

OMG. ❤️ Thank you so much!

@Hungor

Hungor commented Dec 4, 2017

Copy link
Copy Markdown

Thanks!

@jaredfaris

Copy link
Copy Markdown

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

@Antol

Antol commented Jan 17, 2018

Copy link
Copy Markdown

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.

@ezokoze

ezokoze commented Jan 23, 2018

Copy link
Copy Markdown

Thank's man, works like a charm

@Shakkiboy

Copy link
Copy Markdown

@jamescmartinez,
Great piece of work, it really works like a charm, I had one question there are more than hundred member in my team and I have their slack token with me but I have to use 1 token at one time and run this script is there any way to add multiple token in this script so that I can run this script for a group of users ?

Second it is checking for each and every file that was shared with this person and it took long time to delete because some of the files are not shared with this person so script took a lot time, is there any possibility so that script could check only for the files that were uploaded by the use only and delete them ?

Its just a humble request if possible although you have already made our life easy :)

Thank you so much!

@serafimpinto

Copy link
Copy Markdown

Worked just fine! 👍

@marina-mosti

Copy link
Copy Markdown

Great script, thanks

@Petrov-Dmitry

Petrov-Dmitry commented Mar 26, 2018

Copy link
Copy Markdown

I have a trouble... When I running the script, I get a lot of messages like this
"F206K98J3: false"
All files after the script is finished remain not deleted

UPD: Resolved - you must be an admin of your workspace to delete files

@MikeLund

Copy link
Copy Markdown

Anyone modify it to download the file before deleting it? Would be useful, so you can free Slack storage but still have some stuff archived.

@bahaddad

Copy link
Copy Markdown

@MikeLund, it only seems to work with current year files, anything older remains.

@evangs

evangs commented May 8, 2018

Copy link
Copy Markdown

this is great, thanks! I converted to nodejs to better fit my environment. check it out here https://gist.github.com/evangs/85f70573a9046990df4ff820151e0b08

@hez

hez commented May 16, 2018

Copy link
Copy Markdown

If you get rate limited by slack insert a wait on line 22
sleep 1
Found I had to go all the way down to one a second to avoid getting throttled.

@nandodelauni

Copy link
Copy Markdown

I was using the script and worked like a charm, however last week it starting failing and I've no idea of Ruby, maybe I've got Ruby updated or something? It throws away this error:

bulk_slack_files.rb:35:in <main>': undefined method map' for nil:NilClass (NoMethodError)

line 35 is the same as the one shown in the gist file_ids = files.map { |f| f['id'] }

Any clue? Thanks!

@luisenrike

Copy link
Copy Markdown

@nandodelauni I had the same error and it was because I was using an invalid token

@nandodelauni

Copy link
Copy Markdown

thanks @luisenrike!

@ryanckulp

Copy link
Copy Markdown

here's the new URL for generating Legacy workspace tokens:
https://api.slack.com/custom-integrations/legacy-tokens

@savetrev

savetrev commented Jul 17, 2018

Copy link
Copy Markdown

Sorry, I'm not a programmer and am having problems figuring out where to run this code.

Edit: Ahh nevermind, a colleague helped me out. I had to download node.js first :-(

@dominikwilkowski

Copy link
Copy Markdown

I just added a node gist to batch delete files older than a year
https://gist.github.com/dominikwilkowski/76d99ae84848c39e54494f4dbdd4327d

Comes with instructions too :)

@ayhamg

ayhamg commented Oct 1, 2018

Copy link
Copy Markdown

An easier way is to use, no coding required.

https://delete-slack-files.com/

@halburgiss

Copy link
Copy Markdown

I get an HTML doc with little helpful error handling, that starts:

400 ERROR

The request could not be satisfied.

@ayhamg Right now that link is 404.

@danibram

Copy link
Copy Markdown

I just developed a bot that helps you to manage this, any contribution is welcome!
https://github.com/danibram/scrapy-slack-bot

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