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

前端 未结 3 1118
长发绾君心
长发绾君心 2021-02-02 05:09

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

3条回答
  •  生来不讨喜
    2021-02-02 05:49

    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
    

提交回复
热议问题