How To Reduce a Large Heroku Slug Size

Dan Croak

Have you ever seen this error when you tried to deploy to your Heroku app?

Compiled slug size: 500.6M is too large (max is 500M)

You can’t deploy but don’t fret. Here’s a few ways to reduce the slug size.

Clean up the Git repository

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.

Doing this on a recent app reduced my slug size by 100M.

Move some files out of the repo

Move internal design documents (.sketch files) somewhere like Dropbox. Move user-facing media (.mp3, .mpg files) somewhere like Amazon S3.

Ignore some files that have to be in the repo

Follow Heroku’s instructions to ignore files such as unit tests with .slugignore.

Remove unused dependencies

Hopefully each Ruby gem in your Bundler groups is being used by the app, but do a quick audit and remove any that aren’t used.

Bundle only what you need for the environment

Heroku accepts an environment variable to limit the Ruby gems you bundle and cache.

heroku config:set BUNDLE_WITHOUT="development:test" --app your-app-name

Avoid Asset Sync

Rails’ asset pipeline is a tempting area to try to optimize for slug size. App stylesheets, scripts, and images add up.

Asset Sync is a cool tool to push assets to S3 during pre-compilation but Heroku does not recommend it. So far, I’ve been able to avoid Asset Sync.

Good luck!

Hopefully these tips get you deploying again within a few minutes. Good luck!