Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

File.unlink

(*::path file_name) -> ::Integer
File.delete(*paths) → integer
File.unlink(*paths) → integer

Removes the entry (hard link) at each path in paths; returns the count of removed entries:

Example
filepath0 = '/tmp/t0.tmp'
filepath1 = '/tmp/t1.tmp'
File.write(filepath0, 'foo')
File.write(filepath1, 'bar')
File.unlink(filepath0, filepath1) # => 2

If the removed hard link is the last one associated with the inode, also removes the inode; otherwise, not. See Unlinking.

Does not follow symbolic links; if the entry is a symbolic link, removes the entry itself (not the link target).

Example
filepath = '/tmp/t.tmp'
linkpath = '/tmp/link'
File.write(filepath, 'foo')
File.symlink(filepath, linkpath)
File.unlink(linkpath) # => 1
File.exist?(filepath) # => true
File.unlink(filepath) # => 1

Raises an exception on any error; some entries may have been deleted before the error occurs.

See also .umask .utime .truncate .world_readable? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0