65

Could somebody explain to me what the command rake assets:clean really does? Unfortunately the Rails Guides dont mention it. There is also the command rake assets:cleanup. Whats the difference?

Furthermore could somebody tell me when do I have to run rake assets:precompile in production. Do I run it on the server console after I deployed all my application files to my production server? Or do I precompile on my local machine and then do a deploy of all files?

Thanks all

daniel
  • 3,105
  • 4
  • 39
  • 48

7 Answers7

68

Note: This answer is rails 3 specific. For rails 4 and later, look at other answers here.

If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.

But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.

In my projects assets are precompiled as a part of deployment. Capistrano makes it very easy.

Also, I never heard of rake assets:cleanup.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 1
    Hi thx for your answer. Could you explain the step `because it will serve precompiled files instead` to me? You mean when I change these files locally? Whats the difference between precompiled and compiled files? thx man – daniel Feb 17 '12 at 22:13
  • 5
    In your html, links to assets look like '/assets/blah-blah.css`. In development mode this is what happens: webserver looks for `/public/assets/blah-blah.css` and serves it if found. Otherwise, it looks for `/app/assets/stylesheets/blah-blah.scss` and tries to compile and serve it. If you don't precompile assets, you'll always get the latest fresh version of your code. – Sergio Tulentsev Feb 17 '12 at 22:19
  • 1
    Thx man. Your last sentence made me thinking again, but now I get it. So I precompile my assets for production so rails dont need to compile files by itself? – daniel Feb 17 '12 at 22:34
  • This answer is out of date, look for the other ones for up to date information for Rails 4 – hakunin Jul 04 '16 at 07:59
62

Run rake assets:clobber to actually clean the assets. http://www.dixis.com/?p=735

Pete
  • 57,112
  • 28
  • 117
  • 166
  • Thanks! Searched for some time why my CSS changes weren't picked up. Never heard of 'assets:clobber' but it works! – Loed Feb 08 '15 at 08:19
  • hells yeah. i'm not 100% sure but i think that perhaps i noticed assets:clean not working how it used to was when i upgraded to using ruby 2.2.0. I had never heard of assets:clobber before this but it works how assets:clean did in previous versions (1.9.3?) – FireDragon Feb 12 '15 at 03:36
  • This is a lifesaver for me! Spent an hour trying to figure out why my CSS changes weren't being compiled :( – Raymond Feb 29 '16 at 01:31
32

Sergio's answer was completely correct in Rails 3. rake assets:clean deleted all assets that had been previously precompiled into the public/assets directory.

In Rails 4, you run rake assets:clobber to do the same thing.

If you run rake assets:precompile with the following config (by default turned on in staging and production):

# config/environments/production.rb
config.assets.digest = true

You compiled assets get timestamped. This means you can compile your new assets while leaving the old assets in place. You usually want to do this in production so you website will still access the old files while your running precompile to create your new files (because you've added new css/javascript). You now want to get rid of the old files that are no longer in use. The clean it removes the old versions of the precompiled assets while leaving the new assets in place.

grouchomc
  • 472
  • 6
  • 10
8

rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.

cap deploy:clean removes old releases, generally from a remote server. It is not rake assets:clean

rake != cap

paul.belt
  • 289
  • 3
  • 3
0

rake assets:clean is now run by cap deploy:cleanup_assets. Add require 'capistrano/rails/assets' to your Capfile and you get this cap-task. My capistrano version is v3.2.1.

dduft
  • 470
  • 8
  • 18
0
rails assets:clean[keep]
    Remove old compiled assets

rails assets:clobber
    Remove compiled assets

clean deletes previously compiled assets, preserving the current versions

januszm
  • 1,166
  • 13
  • 24
-6

clean up those untracked files with git clean -f for files and git clean -f -d for directories

jaca
  • 1
  • 1