Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a public/cache folder which has files and folders. How can I completely empty that folder using a rake task?

like image 693
Jacob Avatar asked Dec 16 '11 18:12

Jacob


2 Answers

Ruby has the *nix rm -rf equivalent in the FileUtils module that you can use to delete both files and non-empty folders/directories:

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

To keep the directory itself and only remove its contents:

FileUtils.rm_rf(Dir.glob('dir/to/remove/*'))

FileUtils.rm_rf(Dir['dir/to/remove/*'])      # shorter version of above
like image 134
Marek Příhoda Avatar answered Nov 15 '22 12:11

Marek Příhoda


Great answers here already. You might also be interested to know that Rails 5 has a rake task to do this built-in:

rake tmp:cache:clear # Clear cache files from tmp/
like image 3
Gabe Kopley Avatar answered Nov 15 '22 12:11

Gabe Kopley