How to Delete a File in Ruby
Now that we know how to create a file and read from it, let's learn how to delete a file. It can be done with a single line of code.
Guide Tasks
- Read Tutorial
- Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license
Already a Bottega Student? Sign In
Now that we know how to create a file and read from it, let's learn how to delete a file. It can be done with a single line of code, like this:
File.delete("files-lessons/teams.txt")
Execute this code, and go back to your file directory. The file will have been removed.
That's all there is to deleting a file. However, you should be careful while using this command as you don't want to accidentally delete a file. Remember, Ruby offers no warning whatsoever before deleting, so make sure you use it carefully.