How we reduced our Heroku slug size by 50% to deploy again

Compiled slug size: 600.5MB is too large (max is 500MB).

😅😅😅

A few months back we *needed* to deploy a fix to Bento's production application but because our Heroku slug size was too large it was stopping us from actually pushing it up.

This was immensely frustrating at the time but also enlightening. We learned a little bit more about how Heroku works, how to fix issues like this, and how to deploy confidently again as your app eventually gets to the size of Bento!

Attempt #1: Purge!

Install the Heroku Repo plugin:

heroku plugins:install heroku-repo

Then run:

heroku repo:gc --app your-app-name
heroku repo:purge_cache --app your-app-name

These commands will execute git gc --agressive (git-clean) and delete the contents of the Heroku build cache stored in your application’s Git repository. Give deploying another go and see if you can get it up there! For us, this only dropped our slug size by a few mb — not enough!

Attempt #2: Delete!

Over time we had accumulated a lot of files in our repo. Silly stuff like old videos, unoptimised images, and even some sound files from before our Conversations refactor! We removed these and it managed to get our slug down by 50mb — still, not enough!

Attempt #3: RM ALL THE THINGS!  

Turns out, when you have a lot of Javascript packages installed it causes boat. Go figure! 

So, let's remove the node_modules folder after everything is compiled. 

To do this we just have to amend the existing rake assets:clean task a tad. 

namespace :assets do
  desc "Remove 'node_modules' folder"
  task rm_node_modules: :environment do
    Rails.logger.info "Removing node_modules folder"
    FileUtils.remove_dir("node_modules", true)
  end
end

skip_clean = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])

unless skip_clean
  if Rake::Task.task_defined?("assets:clean")
    Rake::Task["assets:clean"].enhance do
      Rake::Task["assets:rm_node_modules"].invoke
    end
  else
    Rake::Task.define_task("assets:clean" => "assets:rm_node_modules")
  end
end

This alone dropped our total slug size by 50% and we could deploy!

Hope one of these solutions helps when you're debugging this issue. 

Goodluck! 
Leave a reply