PHP unlink() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, unlink() deletes a file from the file system.

It is the main way to remove a file in PHP. It works on normal files and on symbolic links. To remove a folder, use rmdir() instead.

Syntax

Call unlink() with the values you need. Start with this line.

PHP
$ok = unlink('old-file.txt');
CallWhat it does
unlink('old-file.txt')Deletes old-file.txt and returns true on success.
unlink(__DIR__ . '/cache/a.tmp')Deletes a file using a full path.
file_exists($f) && unlink($f)Deletes the file only if it exists, avoiding a warning.

unlink() does not move the file to a recycle bin. The deletion is permanent. When the path is a symbolic link, only the link is removed and the target file stays.

Library declarations
PHP
unlink(string $filename, ?resource $context = null): bool

Parameters

ParameterDescription
filenameRequired. The path of the file to delete.
contextOptional. A stream context created with stream_context_create(). Most scripts leave it out.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Delete a File

Create a file, delete it with unlink(), and confirm it is gone.

PHP
<?php
$file = sys_get_temp_dir() . '/unlink_demo.txt';
file_put_contents($file, 'bye');

echo 'Before: ', file_exists($file) ? 'exists' : 'missing', PHP_EOL;

if (unlink($file)) {
    echo 'Deleted', PHP_EOL;
}

echo 'After: ', file_exists($file) ? 'exists' : 'missing', PHP_EOL;

How It Works

  1. file_put_contents() creates a small file and file_exists() confirms it is there.
  2. unlink() deletes the file and returns true.
  3. The success message is printed.
  4. file_exists() now returns false, so the file is gone.

Example 2 — Delete a File That Is Not There

unlink() returns false and shows a warning for a missing file. Check first to avoid it.

PHP
<?php
$file = sys_get_temp_dir() . '/already_gone.txt';

if (is_file($file)) {
    unlink($file);
    echo 'Deleted', PHP_EOL;
} else {
    echo 'Nothing to delete', PHP_EOL;
}

How It Works

  1. is_file() checks that the path is a real file.
  2. The file does not exist, so the else branch runs.
  3. unlink() is never called, so no warning appears.
  4. A short message tells the user that nothing was removed.

Example 3 — Delete All .tmp Files in a Folder

Use glob() to find matching files and delete each one, counting how many were removed.

PHP
<?php
$dir = sys_get_temp_dir() . '/unlink_loop';
mkdir($dir);
file_put_contents($dir . '/a.tmp', '1');
file_put_contents($dir . '/b.tmp', '2');
file_put_contents($dir . '/keep.txt', '3');

$deleted = 0;
foreach (glob($dir . '/*.tmp') as $file) {
    if (unlink($file)) {
        $deleted++;
    }
}

echo "Deleted $deleted files", PHP_EOL;
echo 'keep.txt exists: ', file_exists($dir . '/keep.txt') ? 'yes' : 'no', PHP_EOL;

unlink($dir . '/keep.txt');
rmdir($dir);

How It Works

  1. Three files are created, and only two of them end with . tmp.
  2. glob() with *. tmp returns only the matching files.
  3. unlink() deletes each one and the counter goes up when it succeeds.
  4. keep. txt is not matched, so it stays. The last lines remove it and the folder.

Frequently Asked Questions

It deletes a file. It returns true when the file is deleted and false when something goes wrong.
Call unlink('path/to/file.txt'). Use file_exists() or is_file() first if you want to avoid a warning for a missing file.
No. Use rmdir() for an empty folder. Delete the files inside first with unlink().
The file may not exist, you may not have permission, or on Windows the file may still be open. Close the handle with fclose() and try again.
No. It removes only the link. The target file is left untouched.

Back to Filesystem Functions

Browse all PHP filesystem function tutorials.

Filesystem functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful