Clear Cache After Deploy

16 Jun 2020

Sometimes you use Rails cache in view like this:

<%= render partial: 'products/product', collection: @products, cached: true %>


<% cache product do %>
  <%= render product.games %>
<% end %>

Then you may meet some view content inconsistence issues after deployment. That’s caused by missing run the cache clear task after deploy.

You should run Rails.cache.clear or rails temp:cache:clear after deploy.

If you are using Capistrano to deploy a Rails app, you can write a simple task to do this job:

namespace :deploy do
  after :publishing, :clear_cache do
    invoke 'cache:clear'
  end
end

namespace :cache do
  desc 'Clear Rails cache'
  task :clear do
    on roles(:app) do
      within current_path do
        execute :bundle, 'exec rails runner "Rails.cache.clear"'
      end
    end
  end
end
Back to top