Side Project, Tip

How to Remove All Slack Files Older than 30 Days (With Ruby!)

Over the past year or so I’ve been hosting (moderating?) a private slack channel for some of my friends from high school and previous jobs. What started as a small channel of about five people as since grown into a small community of around forty. While this isn’t an enormous number, it’s large enough that we’ve been eating through our storage space rather quickly. My first thought was to go to the Slack management console, erase a bunch of old files, and be about my day. Unfortunately, unless you’re on a paid tier you can’t manage files very well (you’d have to remove each one individually…and seeing as around 98% of our files were tiny memes that would take forever).

Enter Ruby!

After some Googling around I found this awesome gist that did the bulk of the work for me. I just replaced the throwaway token with my own and saved the script. That should have been the end of it, but I kept getting a series of SSL errors on my Windows 10 machine that wouldn’t let me run the code.

After trying a variety of fixes and wasting about 45 minutes trying to get it working I learned that, apparently, there’s an old issue in Ruby where the net/http library by doesn’t check the validity of an SSL certificate during a TLS handshake. While the fix is very straightforward and easy, it took me a long time to find and figure out, so I decided I’d just write a super short guide for anyone else that had the same problem and hopefully make the solution easier to find.

  1. Download this gist somewhere easy to access—like your Desktop.
  2. Open the slack_delete.rb file and swap the blank token at the top of the file with the one you generate here.
  3. Download this cacert.pem file. (make sure it saves with the .pem extension)
  4. Start a Command Prompt with Ruby.
  5. Change to the directory where yo saved the script from Step 1:  cd FILE_PATH, where FILE_PATH is the file’s location.
  6. Enter the following command: $ set SSL_CERT_FILE=FILE_PATH\cacert.pem replacing FILE_PATH with wherever you saved the cacert.pem file.
  7. Run the Ruby script with ruby slack_delete.rb

Overall, the process is super simple and probably takes in the ballpark of five minutes total. One thing to note though is that as things stand the SSL fix is temporary. You’d have to manually set the SSL_CERT_FILE property manually every time you get the error. I wanted to make the fix permanent so I went a few steps farther:

  1. Save the cacert.pem file somewhere safe—I went ahead and saved it in Ruby23/lib/ruby/2.3.0/rubygems/ssl_certs
  2. Open System in Control Panel.
  3. Click Advanced system settings on left (Windows 10).
  4. Click Environment Variables…
  5. Click New… (I made mine a system variable but I’m pretty sure it doesn’t matter if you make it a user one).
  6. Enter SSL_CERT_FILE under name and then, click Browse File… and select the certificate from Step 1.
  7. Click OK and you should be good to go.

Anyway, while none of the above is simple I couldn’t find it all in one place (the SSL fix on Windows and the Ruby script) so I figured I’d put them together and put it online. Hopefully, this saves someone time!

Leave a Reply

Your email address will not be published. Required fields are marked *