Browse | Submit A New Snippet | Create A Package

 

filesystem recursive deletion

Type:
Function
Category:
File Management
License:
BSD License
Language:
Ruby
 
Description:
A simple script for recursive deletion of a directory. Deletes everything. Originally written to use for a 'clean' target in Rake. The code that actually does the deletion is commented out. Try it. If you decide you trust it, remove the 'puts' and uncomment the File.delete.

Enjoy!

Versions Of This Snippet::

Gary Shea
Snippet ID Download Version Date Posted Author Delete
1100.12005-08-19 17:54Gary Shea

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :0.1

def recursive_delete(dir)
  files = []
  Dir.foreach(dir) do |fname|
    next if fname == '.' || fname == '..'
    path = dir + '/' + fname
    if File.directory?(path)
      puts "dir #{path}"
      recursive_delete(path)
    else
      puts "file #{path}"
      files << path
    end
  end
  files.each do |path|
    puts "delete file #{path}"
    #File.delete(path)
  end
  puts "delete dir #{dir}"
  Dir.rmdir(dir)
end

		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..