1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)unlink.2 8.1 (Berkeley) 6/4/93 29.\" $FreeBSD$ 30.\" 31.Dd September 23, 2020 32.Dt UNLINK 2 33.Os 34.Sh NAME 35.Nm unlink , 36.Nm unlinkat , 37.Nm funlinkat 38.Nd remove directory entry 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In unistd.h 43.Ft int 44.Fn unlink "const char *path" 45.Ft int 46.Fn unlinkat "int dfd" "const char *path" "int flag" 47.Ft int 48.Fn funlinkat "int dfd" "const char *path" "int fd" "int flag" 49.Sh DESCRIPTION 50The 51.Fn unlink 52system call 53removes the link named by 54.Fa path 55from its directory and decrements the link count of the 56file which was referenced by the link. 57If that decrement reduces the link count of the file 58to zero, 59and no process has the file open, then 60all resources associated with the file are reclaimed. 61If one or more process have the file open when the last link is removed, 62the link is removed, but the removal of the file is delayed until 63all references to it have been closed. 64The 65.Fa path 66argument 67may not be a directory. 68.Pp 69The 70.Fn unlinkat 71system call is equivalent to 72.Fn unlink 73or 74.Fn rmdir 75except in the case where 76.Fa path 77specifies a relative path. 78In this case the directory entry to be removed is determined 79relative to the directory associated with the file descriptor 80.Fa dfd 81instead of the current working directory. 82.Pp 83The values for 84.Fa flag 85are constructed by a bitwise-inclusive OR of flags from the following list, 86defined in 87.In fcntl.h : 88.Bl -tag -width indent 89.It Dv AT_REMOVEDIR 90Remove the directory entry specified by 91.Fa fd 92and 93.Fa path 94as a directory, not a normal file. 95.It Dv AT_BENEATH 96Only unlink files and directories which are beneath of the topping 97directory. 98See the description of the 99.Dv O_BENEATH 100flag in the 101.Xr open 2 102manual page. 103.It Dv AT_RESOLVE_BENEATH 104Only walks paths below the topping directory. 105See the description of the 106.Dv O_RESOLVE_BENEATH 107flag in the 108.Xr open 2 109manual page. 110.El 111.Pp 112If 113.Fn unlinkat 114is passed the special value 115.Dv AT_FDCWD 116in the 117.Fa fd 118parameter, the current working directory is used and the behavior is 119identical to a call to 120.Fa unlink 121or 122.Fa rmdir 123respectively, depending on whether or not the 124.Dv AT_REMOVEDIR 125bit is set in flag. 126.Pp 127The 128.Fn funlinkat 129system call can be used to unlink an already-opened file, unless that 130file has been replaced since it was opened. 131It is equivalent to 132.Fn unlinkat 133in the case where 134.Fa path 135is already open as the file descriptor 136.Fa fd . 137Otherwise, the path will not be removed and an error will be returned. 138The 139.Fa fd 140can be set the 141.Dv FD_NONE . 142In that case 143.Fn funlinkat 144behaves exactly like 145.Fn unlinkat . 146.Sh RETURN VALUES 147.Rv -std unlink 148.Sh ERRORS 149The 150.Fn unlink 151succeeds unless: 152.Bl -tag -width Er 153.It Bq Er ENOTDIR 154A component of the path prefix is not a directory. 155.It Bq Er EISDIR 156The named file is a directory. 157.It Bq Er ENAMETOOLONG 158A component of a pathname exceeded 255 characters, 159or an entire path name exceeded 1023 characters. 160.It Bq Er ENOENT 161The named file does not exist. 162.It Bq Er EACCES 163Search permission is denied for a component of the path prefix. 164.It Bq Er EACCES 165Write permission is denied on the directory containing the link 166to be removed. 167.It Bq Er ELOOP 168Too many symbolic links were encountered in translating the pathname. 169.It Bq Er EPERM 170The named file is a directory. 171.It Bq Er EPERM 172The named file has its immutable, undeletable or append-only flag set, see the 173.Xr chflags 2 174manual page for more information. 175.It Bq Er EPERM 176The parent directory of the named file has its immutable or append-only flag 177set. 178.It Bq Er EPERM 179The directory containing the file is marked sticky, 180and neither the containing directory nor the file to be removed 181are owned by the effective user ID. 182.It Bq Er EIO 183An I/O error occurred while deleting the directory entry 184or deallocating the inode. 185.It Bq Er EINTEGRITY 186Corrupted data was detected while reading from the file system. 187.It Bq Er EROFS 188The named file resides on a read-only file system. 189.It Bq Er EFAULT 190The 191.Fa path 192argument 193points outside the process's allocated address space. 194.It Bq Er ENOSPC 195On file systems supporting copy-on-write or snapshots, there was not enough 196free space to record metadata for the delete operation of the file. 197.El 198.Pp 199In addition to the errors returned by the 200.Fn unlink , 201the 202.Fn unlinkat 203may fail if: 204.Bl -tag -width Er 205.It Bq Er EBADF 206The 207.Fa path 208argument does not specify an absolute path and the 209.Fa fd 210argument is neither 211.Dv AT_FDCWD 212nor a valid file descriptor open for searching. 213.It Bq Er ENOTEMPTY 214The 215.Fa flag 216parameter has the 217.Dv AT_REMOVEDIR 218bit set and the 219.Fa path 220argument names a directory that is not an empty directory, 221or there are hard links to the directory other than dot or 222a single entry in dot-dot. 223.It Bq Er ENOTDIR 224The 225.Fa flag 226parameter has the 227.Dv AT_REMOVEDIR 228bit set and 229.Fa path 230does not name a directory. 231.It Bq Er EINVAL 232The value of the 233.Fa flag 234argument is not valid. 235.It Bq Er ENOTDIR 236The 237.Fa path 238argument is not an absolute path and 239.Fa fd 240is neither 241.Dv AT_FDCWD 242nor a file descriptor associated with a directory. 243.It Bq Er ENOTCAPABLE 244.Fa path 245is an absolute path, 246or contained a ".." component leading to a 247directory outside of the directory hierarchy specified by 248.Fa fd , 249and the process is in capability mode. 250.It Bq Er ENOTCAPABLE 251The 252.Dv AT_BENEATH 253flag was provided to 254.Fn unlinkat , 255and the absolute 256.Fa path 257does not have its tail fully contained under the topping directory, 258or the relative 259.Fa path 260escapes it. 261.El 262.Pp 263In addition to the errors returned by 264.Fn unlinkat , 265.Fn funlinkat 266may fail if: 267.Bl -tag -width Er 268.It Bq Er EDEADLK 269The file descriptor is not associated with the path. 270.El 271.Sh SEE ALSO 272.Xr chflags 2 , 273.Xr close 2 , 274.Xr link 2 , 275.Xr rmdir 2 , 276.Xr symlink 7 277.Sh STANDARDS 278The 279.Fn unlinkat 280system call follows The Open Group Extended API Set 2 specification. 281.Sh HISTORY 282The 283.Fn unlink 284function appeared in 285.At v1 . 286The 287.Fn unlinkat 288system call appeared in 289.Fx 8.0 . 290The 291.Fn funlinkat 292system call appeared in 293.Fx 13.0 . 294.Pp 295The 296.Fn unlink 297system call traditionally allows the super-user to unlink directories which 298can damage the file system integrity. 299This implementation no longer permits it. 300