delete()
The unlink() function in PHP is used to delete a file. It's a crucial function for server administrators and web developers who want to manage their files and
How to Delete a File in PHP with unlink()
PHP has no delete() function. To delete a file on the filesystem you use unlink() — the name comes from the underlying Unix system call, which removes a file's name (its "link") from a directory. When the last link is gone, the file's data is freed.
This page covers what unlink() does, the gotchas that trip people up (missing files, permissions, relative paths), and the related functions you reach for when unlink() is not the right tool — such as rmdir() for directories and glob() for deleting many files at once.
Deleting a file (
unlink()) is not the same as deleting database records — for the latter you run an SQLDELETEquery, not a filesystem call.
Syntax
unlink(string $filename, ?resource $context = null): bool$filename— the path to the file you want to delete. It may be absolute (/var/www/data.txt) or relative to the script's working directory (uploads/data.txt).$context— (optional) a stream context, used only with stream wrappers such asftp://. Rarely needed for local files.
unlink() returns true on success and false on failure. On failure it also emits an E_WARNING — for example when the file does not exist or the process lacks permission to remove it.
Always check the result
A bare unlink("file.txt") silently leaves a warning in your logs if the file is missing. Check the return value and, when the file may not exist, guard with file_exists() first so you do not trigger a warning:
$path = "report.txt";
if (file_exists($path)) {
if (unlink($path)) {
echo "Deleted $path\n";
} else {
echo "Could not delete $path (check permissions)\n";
}
} else {
echo "Nothing to delete: $path does not exist\n";
}Examples
Example 1: Create a file, then delete it
This self-contained example creates a temporary file, confirms it exists, deletes it with unlink(), and confirms it is gone:
$path = sys_get_temp_dir() . "/w3docs-demo.txt";
file_put_contents($path, "temporary data");
echo "Exists before delete? " . (file_exists($path) ? "yes" : "no") . "\n";
unlink($path);
echo "Exists after delete? " . (file_exists($path) ? "yes" : "no") . "\n";Output:
Exists before delete? yes
Exists after delete? noExample 2: Delete every matching file with glob()
unlink() deletes one file per call, so to clear out a set of files combine it with glob(), which returns an array of matching paths:
foreach (glob("/tmp/cache/*.tmp") as $file) {
unlink($file);
}This removes every .tmp file in /tmp/cache. The same pattern is the standard way to "empty" a directory of files before removing the directory itself with rmdir() (which only works on empty directories).
Example 3: Delete a file over a stream wrapper
unlink() works with stream wrappers, including remote ones. The optional $context lets you pass wrapper-specific options:
$context = stream_context_create([
'ftp' => ['overwrite' => true],
]);
unlink("ftp://example.com/old-export.txt", $context);Common pitfalls
- The file does not exist.
unlink()returnsfalseand warns. Usefile_exists()or suppress and check:@unlink($path). - Permissions. Deletion depends on write permission on the containing directory, not the file itself. The PHP process (often
www-data) must own or have write access to that directory. - Relative paths. A relative path resolves against the current working directory, which is not always the script's folder. Prefer absolute paths or build them with
__DIR__. - It is a directory.
unlink()cannot remove directories — usermdir()for an empty directory, and delete its contents first. - The file is open. On Windows, deleting a file that is still open by a handle fails; close it first. On Unix the unlink succeeds but the data lingers until the handle closes.
Related functions
rmdir()— remove an empty directory.file_exists()— test whether a path exists before deleting.rename()— move or rename a file instead of deleting it.copy()— copy a file before overwriting or removing the original.glob()— list files matching a pattern, then delete them in a loop.
Conclusion
Use unlink() to delete a single file, always check its return value, and guard with file_exists() when the file may be absent. For directories reach for rmdir(), and for batch deletes pair unlink() with glob().