# Deleting a non empty directory

**URL:** <https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167>\
**Category:** Ruby\
**Created:** [June 18, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167 "2007-06-18T19:31:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 18, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167/1 "2007-06-18T19:31:56Z")

</div>

Hi, I am getting an exception when I use Dir.delete on a non empty  
directory.  
What should I do to remove a non empty directory?

Thanks,  
Kostas

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 18, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167/2 "2007-06-18T20:30:58Z")

</div>

On Jun 18, 1:32 pm, Kostas Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, I am getting an exception when I use Dir.delete on a non empty  
> directory.  
> What should I do to remove a non empty directory?
> 
> Thanks,  
> Kostas
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Use Dir.each to iterate over the items in the dir, and call  
File.delete on them. Then call Dir.delete.

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 18, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167/3 "2007-06-18T20:41:00Z")

</div>

On Jun 18, 8:28 am, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> Use Dir.each to iterate over the items in the dir, and call  
> File.delete on them. Then call Dir.delete.

Or use FileUtils::rm\_rf:

[http://www.ruby-doc.org/core/classes/FileUtils.html#M004368](http://www.ruby-doc.org/core/classes/FileUtils.html#M004368)

Be careful though. 😉

b

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 18, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/deleting-a-non-empty-directory/107167/4 "2007-06-18T22:47:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Jun 18, 8:28 am, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > –  
> > > Posted viahttp://www.ruby-forum.com/.
> > 
> > Use Dir.each to iterate over the items in the dir, and call  
> > File.delete on them. Then call Dir.delete.
> 
> Or use FileUtils::rm\_rf:
> 
> [http://www.ruby-doc.org/core/classes/FileUtils.html#M004368](http://www.ruby-doc.org/core/classes/FileUtils.html#M004368)
> 
> Be careful though. 😉
> 
> b

Thanks a lot!
