meallhour
meallhour

Reputation: 15571

Deleting file at end of Chef run

I want to delete file at the end of Chef run.
I am using the following file resource to achieve that

file "#{home_dir['data']}/file1.txt" do
  action :delete
  retries 3
  retry_delay 50
  only_if { ::File.exist? "#{home_dir['data']}/file1.txt" }
end

Please let me know if there is a better way to achieve it.

Upvotes: 1

Views: 1930

Answers (1)

coderanger
coderanger

Reputation: 54181

This is correct, but you don't need the only_if, the file resource is internally idempotent already. You also probably don't need the retries unless you actually expect unlink() to fail (like if this is a weird network file system), but it doesn't hurt anything.

EDIT: How to avoid resource cloning if you have the same resource type+name in your recipes twice.

file "delete #{home_dir['data']}/file1.txt (or some other unique name)" do
  path "#{home_dir['data']}/file1.txt"
  action :delete
end

The path property defaults to use the name if not specified, but they can be changed independently to avoid collisions.

Upvotes: 1

Related Questions