Five Secret Heroku Slug Cleaning Commands

The Five Secret Heroku Slug Cleaning Commands

This is to fix build, slug, and caching problems related to asset compilation during slug compilation.

Everyone needs a little spring cleaning, right? No, really. Sometimes when I switch around buildpacks or change up assets I run into a strange asset cache problem. Here’s the five magic commands to purge your slug compile process, from less-invasive to more-invasive. When you have a nasty asset cache problem, usually #4 is the one you need.

To use the Heroku repo plugin, I think you’ll want to install it following these instructions.
Note that the Heroku plugins are installed LOCALLY on your machine not on your environment. They are simply shell scripts that perform a series of actions on a remote Heroku environment.

Getting

 The same version of this code has already been built at least twice. One common cause of this behavior is attempting to deploy code from a different branch.

If you push to Heroku during one of Heroku’s platform downtime events, which you will see on the Heroku Status page, you may have initialized a build where your SHA (the last Git commit) was built but the platform did not deploy it correctly. Heroku has detected that you’ve already built this SHA and will not rebuild it for you. (The fixes below will not fix such a problem.) Instead, you can easily fix this simply by pushing an empty commit:

git commit --allow-empty -m "empty commit"

Then repush to Heroku and this new SHA will generate a new build, even though your code has no changes.

The Secret Slug Cleaning Commands

I call these “secret” because for some reason Heroku makes these difficult to find in their docs. Use with caution. After each one, re-push your branch to Heroku.

Remember for all of these commands, replace <appname> with your Heroku app name.

  1. Clean the Rails cache

Rails 5 & above:

heroku run rails tmp:cache:clear -a <appname>

(For Rails 4 use heroku run rake tmp:cache:clear instead)

(then push your branch to Heroku)

2. Clobber the assets

heroku run rails assets:clobber -a <appname>

(Rails 4+, for Rails < 3, use heroku run rake assets:clean instead)
(then push your branch to Heroku)

3. Aggressive git clean

heroku repo:gc -a <appname>

This will run a git gc -agressive against the applications repo.

(then push your branch to Heroku)

4. Purge the cache

heroku repo:purge_cache -a <appname>

This will delete the contents of the build cache stored in the repository. This is done inside a run process on the application.
(then push your branch to Heroku)

5. Full repository reset:

heroku repo:reset -a <appname>

This will fully empty the remote repository.
(then push your branch to Heroku)

Leave a Reply

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

Previous post How to get your Heroku app to optimize your images for you automagically using Paperclip, image_optim and special buildpacks
Next post Port YAML-based configs to Client-Side Code as JSON