Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

FileUtils#rm_r

rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)

Removes entries at the paths in the given list (a single path or an array of paths); returns list, if it is an array, [list] otherwise.

Argument list or its elements should be interpretable as paths.

May cause a local vulnerability if not called with keyword argument secure: true; see Avoiding the TOCTTOU Vulnerability.

For each file path, removes the file at that path:

Example
FileUtils.touch(['src0.txt', 'src0.dat'])
FileUtils.rm_r(['src0.dat', 'src0.txt'])
File.exist?('src0.txt') # => false
File.exist?('src0.dat') # => false

For each directory path, recursively removes files and directories:

Example
tree('src1')
# => src1
#    |-- dir0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- dir1
#        |-- src2.txt
#        `-- src3.txt
FileUtils.rm_r('src1')
File.exist?('src1') # => false

Keyword arguments:

  • force: true - ignores raised exceptions of StandardError and its descendants.

  • noop: true - does not remove entries; returns nil.

  • secure: true - removes src securely; see details at FileUtils.remove_entry_secure.

  • verbose: true - prints an equivalent command:

    Example
    FileUtils.rm_r(['src0.dat', 'src0.txt'], noop: true, verbose: true)
    FileUtils.rm_r('src1', noop: true, verbose: true)
    

    Output:

    rm -r src0.dat src0.txt
    rm -r src1

Related: methods for deleting.

See also .rm_r #rm_f #rm_rf #rmdir also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0