tags:

views:

121

answers:

4

I'd like to delete a directory that may or may not contain files or other directories. Looking in the Ruby docs I found Dir.rmdir but it won't delete non-empty dir. Is there a convenience method that let's me do this? Or do I need to write a recursive method to examine everything below the directory?

A: 

The laziest way is:

def delete_all(path)
    `rm -rf "#{path}"`
end
Paul Betts
+1  A: 

On non-Windows systems, the following will work:

system("rm -rf #{dir}")
Denis Hennessy
You forgot quotes; what if the path is /home/bob/Some Other Dir ?
Paul Betts
+16  A: 
require 'fileutils'

FileUtils.rm_rf(dir)
womble
A: 

A pure Ruby way:

require 'fileutils'

FileUtils.rm_rf("/directory/to/go")

If you need thread safety: (warning, changes working directory)

FileUtils.rm_rf("directory/to/go", :secure=>true)

kraryal