How can I clear rails cache after deploy to heroku?

46,219

Solution 1

Rails has a built in rake task:

rake tmp:clear

Solution 2

The following should work on cedar:

heroku run console

..then wait 5 seconds for heroku console to boot

Rails.cache.clear

Then you should see the cache clear, and you can quit console. Remember you might have to refresh a few times because your local machine will often cache assets and such in your browser until it makes a fresh request.

If it happens to be assets that you're caching though, you don't need to go through a manual clear every time you push, you just need to have the asset pipeline set up and make sure all your js/css(less/sass)/static images are being compiled with hashes at the end of their filenames.

Solution 3

You should be able to create a cache clearing rake task that looks something like this:

namespace :cache do
  desc "Clears Rails cache"
  task :clear => :environment do
    Rails.cache.clear
  end
end

and invoke it directly in one command that you can use in your post deploy hook - like so:

heroku run rake cache:clear

Solution 4

Ruby on Rails has a magical ENV variable called 'RAILS_CACHE_ID'. I set this to the git commit id whenever I deploy: heroku config:set RAILS_CACHE_ID=$CIRCLE_SHA1

Solution 5

If you want to simply run a rake task after deploy, I would recommend checking out:

https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

We've been using it in production for over 6 months and it's been rock solid.

First, add the buildpack AFTER you already have the Ruby buildpack set. This should happen after your first deploy to the server

heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

Second, set an environment variable called DEPLOY_TASKS with just the names of the rake tasks you want to run separated by spaces.

heroku config:set DEPLOY_TASKS='cache:clear

Share:
46,219
kikiki.blue
Author by

kikiki.blue

Updated on March 09, 2021

Comments

  • kikiki.blue
    kikiki.blue about 3 years

    I applied cache to my heroku rails app and it works well. But everytime I deploy to heroku, I also want to clear cache automatically.

    so I googled and I found this.

    task :after_deploy, :env, :branch do |t, args|
      puts "Deployment Complete"
      puts "Cleaning all cache...."
      FileUtils.cd Rails.root do
        sh %{heroku run console}
        sh %{Rails.cache.clear}
      end
    end
    

    but when I raked this script, it just show the heroku console command line but the Rails.cache.clear command does not typed. (I guess that is because heroku console is interactive)

    and

    system "heroku console Rails.cache.clear"
    

    doesn't work for cedar apps.

    How can I solve this problem?

    Thanks.

  • kikiki.blue
    kikiki.blue over 11 years
    but as you know, the script what I wrote is rake script. I know that the command 'heroku run console' returns after 5 seconds and I can type 'Rails.cache.clear'. but I want to automate it with my rake script everytime I deploy to heroku.
  • a.ross.cohen
    a.ross.cohen over 11 years
    follow this pattern michaeldwan.com/writings/customize-your-heroku-deployment.ht‌​ml and then add an :after_deploy hook that just calls Rails.cache.clear.
  • kikiki.blue
    kikiki.blue over 11 years
    Yes I saw that pattern. but I don't know how can I connect to heroku console and call Rails.cache.clear.
  • steve
    steve over 7 years
    TIL RAILS_CACHE_ID is a thing. Very cool. Adjusted for those of us not using circle ci: heroku config:set RAILS_CACHE_ID=git rev-parse HEAD``
  • foobar443
    foobar443 about 7 years
    This is the best approach and should be marked as accepted answer. This solution does not imply manual action nor does it imply to modify the project itself.
  • Richard Peck
    Richard Peck almost 7 years
    As a note, this has been deprecated in favour of Heroku's "Release Phase" functionality