Instantly share code, notes, and snippets.

Embed
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!'
@jackcarter

This comment has been minimized.

Show comment
Hide comment
@jackcarter

jackcarter Jan 4, 2016

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

Thanks James!

jackcarter commented Jan 4, 2016

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

Thanks James!

@jamescmartinez

This comment has been minimized.

Show comment
Hide comment
@jamescmartinez
Owner

jamescmartinez commented Jan 14, 2016

No problem @jackcarter!

@drew2a

This comment has been minimized.

Show comment
Hide comment
@drew2a

drew2a commented Mar 14, 2016

Thanks!

@pimschaaf

This comment has been minimized.

Show comment
Hide comment
@pimschaaf

pimschaaf Mar 15, 2016

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

pimschaaf commented Mar 15, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@jkbz

jkbz 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 :)

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 :)

@mdunbavan

This comment has been minimized.

Show comment
Hide comment
@mdunbavan

mdunbavan Apr 13, 2016

What about private files?

mdunbavan commented Apr 13, 2016

What about private files?

@plowzzer

This comment has been minimized.

Show comment
Hide comment
@plowzzer

plowzzer Apr 15, 2016

Worked =D Thanks bro

plowzzer commented Apr 15, 2016

Worked =D Thanks bro

@hanpannet

This comment has been minimized.

Show comment
Hide comment
@hanpannet

hanpannet 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!

hanpannet 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!

@soygul

This comment has been minimized.

Show comment
Hide comment

soygul commented May 10, 2016

@dilzio

This comment has been minimized.

Show comment
Hide comment
@dilzio

dilzio May 11, 2016

worked like a charm...thanks! :)

dilzio commented May 11, 2016

worked like a charm...thanks! :)

@hanpannet

This comment has been minimized.

Show comment
Hide comment
@hanpannet

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

hanpannet 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

This comment has been minimized.

Show comment
Hide comment
@GR4YSON

GR4YSON May 27, 2016

This works like a charm! Thank you so much.

GR4YSON commented May 27, 2016

This works like a charm! Thank you so much.

@simonelberts

This comment has been minimized.

Show comment
Hide comment
@simonelberts

simonelberts Jun 9, 2016

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

simonelberts commented Jun 9, 2016

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

@robertstinnett

This comment has been minimized.

Show comment
Hide comment
@robertstinnett

robertstinnett Jul 20, 2016

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.

robertstinnett commented Jul 20, 2016

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

This comment has been minimized.

Show comment
Hide comment
@lyrixx

lyrixx commented Aug 2, 2016

And the PHP version

@bryanjacquot

This comment has been minimized.

Show comment
Hide comment
@bryanjacquot

bryanjacquot Aug 8, 2016

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

bryanjacquot commented Aug 8, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@nickell

nickell Aug 10, 2016

@bryanjacquot Same issue here, any luck yet?

nickell commented Aug 10, 2016

@bryanjacquot Same issue here, any luck yet?

@xarses

This comment has been minimized.

Show comment
Hide comment
@xarses

xarses 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)

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)

@CharlieIGG

This comment has been minimized.

Show comment
Hide comment
@CharlieIGG

CharlieIGG Oct 6, 2016

This is awesome, thank you.

CharlieIGG commented Oct 6, 2016

This is awesome, thank you.

@leoduquesnel

This comment has been minimized.

Show comment
Hide comment
@leoduquesnel

leoduquesnel Oct 21, 2016

It worked for me!

leoduquesnel commented Oct 21, 2016

It worked for me!

@tgensol

This comment has been minimized.

Show comment
Hide comment
@tgensol

tgensol commented Nov 4, 2016

any news @xarses ?

@LiquidRod

This comment has been minimized.

Show comment
Hide comment
@LiquidRod

LiquidRod Nov 13, 2016

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.

LiquidRod commented Nov 13, 2016

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.

@jwilkinson

This comment has been minimized.

Show comment
Hide comment
@jwilkinson

jwilkinson Dec 7, 2016

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

jwilkinson commented Dec 7, 2016

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

@drewlustro

This comment has been minimized.

Show comment
Hide comment
@drewlustro

drewlustro commented Jan 28, 2017

thank you.

@oleyb

This comment has been minimized.

Show comment
Hide comment
@oleyb

oleyb Mar 10, 2017

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

oleyb commented Mar 10, 2017

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

@vadimpeskov

This comment has been minimized.

Show comment
Hide comment
@vadimpeskov

vadimpeskov Apr 5, 2017

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

vadimpeskov commented Apr 5, 2017

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

@diegodurante

This comment has been minimized.

Show comment
Hide comment
@diegodurante

diegodurante Apr 10, 2017

@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

diegodurante commented Apr 10, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@kenshin6

kenshin6 Apr 11, 2017

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

kenshin6 commented Apr 11, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@theapplepastor

theapplepastor May 16, 2017

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?

theapplepastor commented May 16, 2017

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

This comment has been minimized.

Show comment
Hide comment
@tomleo

tomleo 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. ☮️

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

This comment has been minimized.

Show comment
Hide comment
@AknEp

AknEp 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).

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

This comment has been minimized.

Show comment
Hide comment
@icfantv

icfantv Oct 28, 2017

OMG. ❤️ Thank you so much!

icfantv commented Oct 28, 2017

OMG. ❤️ Thank you so much!

@Hungor

This comment has been minimized.

Show comment
Hide comment
@Hungor

Hungor commented Dec 4, 2017

Thanks!

@jaredfaris

This comment has been minimized.

Show comment
Hide comment
@jaredfaris

jaredfaris Jan 3, 2018

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

jaredfaris commented Jan 3, 2018

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

@Antol

This comment has been minimized.

Show comment
Hide comment
@Antol

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

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.

@DrFitch

This comment has been minimized.

Show comment
Hide comment
@DrFitch

DrFitch Jan 23, 2018

Thank's man, works like a charm

DrFitch commented Jan 23, 2018

Thank's man, works like a charm

@Shakkiboy

This comment has been minimized.

Show comment
Hide comment
@Shakkiboy

Shakkiboy Feb 4, 2018

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

Shakkiboy commented Feb 4, 2018

@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

This comment has been minimized.

Show comment
Hide comment
@serafimpinto

serafimpinto Mar 7, 2018

Worked just fine! 👍

serafimpinto commented Mar 7, 2018

Worked just fine! 👍

@immarina

This comment has been minimized.

Show comment
Hide comment
@immarina

immarina Mar 14, 2018

Great script, thanks

immarina commented Mar 14, 2018

Great script, thanks

@Petrov-Dmitry

This comment has been minimized.

Show comment
Hide comment
@Petrov-Dmitry

Petrov-Dmitry Mar 26, 2018

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

Petrov-Dmitry commented Mar 26, 2018

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

This comment has been minimized.

Show comment
Hide comment
@MikeLund

MikeLund Mar 28, 2018

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.

MikeLund commented Mar 28, 2018

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

This comment has been minimized.

Show comment
Hide comment
@bahaddad

bahaddad Apr 21, 2018

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

bahaddad commented Apr 21, 2018

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

@evangs

This comment has been minimized.

Show comment
Hide comment
@evangs

evangs May 8, 2018

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

evangs commented May 8, 2018

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

@hez

This comment has been minimized.

Show comment
Hide comment
@hez

hez May 16, 2018

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.

hez commented May 16, 2018

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

This comment has been minimized.

Show comment
Hide comment
@nandodelauni

nandodelauni May 31, 2018

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 methodmap' 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!

nandodelauni commented May 31, 2018

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 methodmap' 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

This comment has been minimized.

Show comment
Hide comment
@luisenrike

luisenrike May 31, 2018

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

luisenrike commented May 31, 2018

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

@nandodelauni

This comment has been minimized.

Show comment
Hide comment
@nandodelauni

nandodelauni commented Jun 6, 2018

thanks @luisenrike!

@ryanckulp

This comment has been minimized.

Show comment
Hide comment
@ryanckulp

ryanckulp Jun 8, 2018

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

ryanckulp commented Jun 8, 2018

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

@savetrev

This comment has been minimized.

Show comment
Hide comment
@savetrev

savetrev Jul 17, 2018

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 :-(

savetrev commented Jul 17, 2018

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

This comment has been minimized.

Show comment
Hide comment
@dominikwilkowski

dominikwilkowski Sep 21, 2018

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

Comes with instructions too :)

dominikwilkowski commented Sep 21, 2018

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

This comment has been minimized.

Show comment
Hide comment
@ayhamg

ayhamg Oct 1, 2018

An easier way is to use, no coding required.

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

ayhamg commented Oct 1, 2018

An easier way is to use, no coding required.

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

@halburgiss

This comment has been minimized.

Show comment
Hide comment
@halburgiss

halburgiss Oct 16, 2018

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.

halburgiss commented Oct 16, 2018

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.

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