chief
chief

Reputation: 740

Delete a directory from a script

I have a ruby script that runs as a daemon process. After a new record is saved I want to delete all folders and files in the cache directory. I have tried the code below with no success:

 require 'fileutils'

 @report.save
 FileUtils.rm_rf("absolute_path/tmp/cache/.")
 # also tried
 # FileUtils.rm_rf("#{RAILS.root}/tmp/cache/.")

Upvotes: 1

Views: 166

Answers (1)

Chris Cherry
Chris Cherry

Reputation: 28574

FileUtils.rm_rf doesn't accept wildcards on its own. If you want want keep the cache directory itself, and remove its contents only, use a glob:

FileUtils.rm_rf Dir.glob('absolute_path/tmp/cache/*')

Upvotes: 2

Related Questions