End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
FileUtils.rm_r
# File tmp/rubies/ruby-3.2.0/lib/fileutils.rb, line 1315
def rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)
list = fu_list(list)
fu_output_message "rm -r#{force ? 'f' : ''} #{list.join ' '}" if verbose
return if noop
list.each do |path|
if secure
remove_entry_secure path, force
else
remove_entry path, force
end
end
end
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:
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:
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 ofStandardErrorand its descendants. -
noop: true- does not remove entries; returnsnil. -
secure: true- removessrcsecurely; see details atFileUtils.remove_entry_secure. -
verbose: true- prints an equivalent command:ExampleFileUtils.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.