unlink(2)

okay so first things first โ€” this is the unlink(2) page from the Linux system calls manual. Two functions, one mission: making names disappear. no cap. ๐Ÿซถ


๐ŸŒธ NAME ๐ŸŒธ

unlink, unlinkat โ€” delete a name and possibly the file it refers to

like... the name goes. the file maybe goes. it's giving "it's complicated" ๐Ÿ˜ญ


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc)


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <unistd.h>

int unlink(const char *path);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h>

int unlinkat(int dirfd, const char *path, int flags);

โœจ Feature Test Macro Requirements for glibc โœจ

(peep feature_test_macros(7) if you need the whole lore)

unlinkat():
    Since glibc 2.10:
        _POSIX_C_SOURCE >= 200809L
    Before glibc 2.10:
        _ATFILE_SOURCE

๐Ÿ’– DESCRIPTION ๐Ÿ’–

ok so unlink() deletes a name from the filesystem. that's the whole vibe. ๐Ÿ”ฅ

if that name was the last link to a file and no processes have the file open โ€” boom, the file is deleted and the space it was using is made available for reuse. gone. recycled. ๐Ÿ—‘๏ธโœจ

if the name was the last link to a file but some processes still have that file open? the file stays alive until the last file descriptor referring to it is closed. she's not dead, she's just... unreferenced by name. main character energy tbh. ๐Ÿ‘‘

if the name referred to a symbolic link, the link itself is removed.

if the name referred to a socket, FIFO, or device โ€” the name for it gets removed, but processes which have the object open may continue to use it. so like, the nametag is gone but everybody's still at the party. ๐ŸŽ‰

๐ŸŒˆ unlinkat()

unlinkat() operates in exactly the same way as either unlink() or rmdir(2) โ€” depending on whether or not flags includes the AT_REMOVEDIR flag โ€” except for the differences described here. (yes, she's a two-for-one. iconic.)

  • if path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd โ€” rather than relative to the current working directory of the calling process, which is what unlink() and rmdir(2) do for a relative pathname.
  • if path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process โ€” just like unlink() and rmdir(2) do. so basically the default behaviour, bestie.
  • if path is absolute, then dirfd is ignored. sorry dirfd, not your moment. ๐Ÿ™ˆ

flags is a bit mask that can either be specified as 0, or by ORing together flag values that control the operation of unlinkat(). currently, only one such flag is defined:

  • AT_REMOVEDIR โ€” by default, unlinkat() performs the equivalent of unlink() on path. if the AT_REMOVEDIR flag is specified, it performs the equivalent of rmdir(2) on path. (so it's a directory deleter now. versatile. we love her.)
peep openat(2) for an explanation of the need for unlinkat(). iykyk ๐Ÿ’œ

โญ RETURN VALUE โญ

  • on success: zero is returned. slay. ๐ŸŽ‰
  • on error: -1 is returned, and errno is set to indicate the error. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

buckle up bestie, this is the long part. every single one of these matters fr fr.

  • EACCES โ€” write access to the directory containing path is not allowed for the process's effective UID, or one of the directories in path did not allow search permission. (see also path_resolution(7).)
  • EBUSY โ€” path cannot be unlinked because it is being used by the system or another process; for example, it is a mount point or the NFS client software created it to represent an active but otherwise nameless inode ("NFS silly renamed"). she's busy, she said not right now. ๐Ÿ’…
  • EFAULT โ€” path points outside your accessible address space.
  • EIO โ€” an I/O error occurred.
  • EISDIR โ€” path refers to a directory. (this is the non-POSIX value returned since Linux 2.1.132.)
  • ELOOP โ€” too many symbolic links were encountered in translating path.
  • ENAMETOOLONG โ€” path was too long. ๐Ÿ˜ญ
  • ENOENT โ€” a component in path does not exist or is a dangling symbolic link, or path is empty.
  • ENOMEM โ€” insufficient kernel memory was available.
  • ENOTDIR โ€” a component used as a directory in path is not, in fact, a directory.
  • EPERM โ€” the system does not allow unlinking of directories, or unlinking of directories requires privileges that the calling process doesn't have. (this is the POSIX prescribed error return; as noted above, Linux returns EISDIR for this case.)
  • EPERM (Linux only) โ€” the filesystem does not allow unlinking of files.
  • EPERM or EACCES โ€” the directory containing path has the sticky bit (S_ISVTX) set and the process's effective UID is neither the UID of the file to be deleted nor that of the directory containing it, and the process is not privileged (Linux: does not have the CAP_FOWNER capability).
  • EPERM โ€” the file to be unlinked is marked immutable or append-only. (see FS_IOC_SETFLAGS(2const).)
  • EROFS โ€” path refers to a file on a read-only filesystem. ๐ŸงŠ

the same errors that occur for unlink() and rmdir(2) can also occur for unlinkat(). the following additional errors can occur for unlinkat():

  • EBADF โ€” path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
  • EINVAL โ€” an invalid flag value was specified in flags.
  • EISDIR โ€” path refers to a directory, and AT_REMOVEDIR was not specified in flags.
  • ENOTDIR โ€” path is relative and dirfd is a file descriptor referring to a file other than a directory.

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2024.


๐Ÿงš HISTORY ๐Ÿงš

  • unlink() โ€” SVr4, 4.3BSD, POSIX.1-2001.
  • unlinkat() โ€” POSIX.1-2008. Linux 2.6.16, glibc 2.4.

๐ŸŒŸ glibc ๐ŸŒŸ

on older kernels where unlinkat() is unavailable, the glibc wrapper function falls back to the use of unlink() or rmdir(2). and when path is relative, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument. resourceful queen. ๐Ÿ’œ


๐Ÿ“ BUGS ๐Ÿ“

infelicities in the protocol underlying NFS can cause the unexpected disappearance of files which are still being used. so like... your file just vanishes?? while you're using it?? yeah. nfs said "surprise". ๐Ÿ’€


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

rm(1), unlink(1), chmod(2), link(2), mknod(2), open(2), rename(2), rmdir(2), mkfifo(3), remove(3), path_resolution(7), symlink(7)


okay that's the whole page bestie, go forth and delete responsibly ๐Ÿซถโœจ

this was Linux man-pages 6.20, dated 2026-02-08 โ€” mwah, kisses, take care of her ๐Ÿ’…๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms