How to Delete a File in Ruby

The following tutorial describe how to delete files and directories that you no longer need in your application. Removing single file in Ruby is simple and straightforward, with File.delete. This method Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error.

Removing a directory tree is also straightforward and simple with FileUtils.remove_dir, which recursively deletes the contents of a directory. This method ignores StandardError if force(second parameter) is true.

Ruby provides several methods for removing directories, but mostly we use remove_dir. Dir.delete and FileUtils.rmdir will only work if the directory is already empty. The rm_r and rm_rf defined in FileUtils are similar to remove_dir.

Categorized as Ruby

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments