25

Attempting to delete a non-empty folder:

directory "C:\tempdirectory" do
 action :delete
end

... in Chef I receive:

Errno::ENOTEMPTY
Directory not empty

Is there a quick way to delete all files in the directory?

Or an argument or flag to allow me to delete non-empty directories?

2
  • I assume you're using a simple :delete action on a standard directory resource block?
    – Patrick M
    Commented Apr 18, 2014 at 15:26
  • Yes, sorry editing to add code
    – DonBecker
    Commented Apr 18, 2014 at 15:31

2 Answers 2

53

If I recall correctly, setting the recursive true attribute will force remove non-empty directories.

The docs for the directory LWRP don't describe this behavior, but they do provide this usage example:

directory "/tmp/something" do
  recursive true
  action :delete
end

The docs have since been amended to cryptically say:

recursive
Ruby Types: TrueClass, FalseClass

Create or delete parent directories recursively. For the owner, group, and mode properties, the value of this attribute applies only to the leaf directory.
Default value: false.

They still stop short of saying "recursive true is required to delete non-empty directories. Without this setting, attempting to delete a non-empty directory will fail with the message: Errno::ENOTEMPTY Directory not empty".

2
  • 2
    Thanks, works great. Didn't see any note about it in the docs.
    – DonBecker
    Commented Apr 18, 2014 at 15:31
  • 2
    Yeah, it's not mentioned in there at all.
    – Patrick M
    Commented Apr 18, 2014 at 15:46
5

You have to add the recursive true option. This option will remove all the directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.