How to delete all contents of a folder using Ruby-Rails?

I have a public/cache folder that has files and folders in it. How can I completely delete this folder using the rake task?

+53
ruby ruby-on-rails-3
Dec 16 '11 at 18:19
source share
2 answers

Ruby has * nix rm -rf in the FileUtils module, which you can use to delete both files and non-empty folders

 FileUtils.rm_rf('dir/to/remove') 

To save the directory itself and delete its contents:

 FileUtils.rm_rf(Dir.glob('dir/to/remove/*')) 
+128
Dec 16 '11 at 18:25
source share

You can run arbitrary commands using the slanted single quote (next to the tilde):

 `rm -fr public/cache/*` 

It may be more platform dependent than you want, but it opens up many possibilities.

+2
Jan 27 '14 at 17:10
source share



All Articles