I want to delete the directory that contains the images created by CarrierWave.

Hierarchy public/uploads/aaa/image/1/test1.jpeg
In this case, delete /1/test1.jpeg.

carrierwaveremove_image!Then only the image is deleted and the directory with the id name remains as it is.
I want to delete this all.

Corresponding source code
  # @ product.remove_image! This is just an image
    # File.delete ("") I thought I could delete it with this, but it will be No such file because the relative path is specified incorrectly.
What I tried

Remove the path by @ aaa.image.url and pass it to File.delete. → No such file error

File.delete How should I specify the relative path? Is it relative as seen from the controller?

  • Answer # 1

    In the first place, File.delete can only delete files, not the entire contents of the directory.
    fileutilsSolved by using.

    dir = Rails.root + "dir"
    FileUtils.rm_rf ("dir")