PHP delete Alias

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, there is no real function called delete(). The manual page for delete only points you to unlink(), which removes a file, and unset(), which removes a variable.

To delete a file in PHP, call unlink() with the path of the file. It works like an alias that beginners often look for, so the examples here use unlink() the same way you would use a delete command.

Syntax

Call delete with the values you need. Start with this line.

PHP
$ok = unlink(__DIR__ . "/demo.txt");
CallWhat it does
unlink("demo.txt")Deletes the file demo.txt and returns true or false.
unlink(__DIR__ . "/temp/cache.tmp")Deletes a file using a full path built from the script folder.
unset($name)Removes a variable, not a file. This is the other meaning of delete in PHP.

delete is a documentation entry only. Calling delete() in your code causes a fatal error for an undefined function, so always use unlink() for files and rmdir() for empty folders.

Library declarations
PHP
delete(string $filename): bool // not a real function, use unlink(string $filename, ?resource $context = null): bool

Parameters

ParameterDescription
filenameRequired. The path of the file to delete. This is the argument that unlink() takes.
contextOptional. A stream context, supported by unlink() only. Rarely needed.

Return Value

Return ValueDescription
boolunlink() returns true if the file was deleted, or false if it failed.

PHP Examples

Example 1 — Delete a File

Create a temporary file and remove it with unlink(), which is the PHP way to delete a file.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "Temporary data");

echo "Exists: ", file_exists($file) ? "yes" : "no", PHP_EOL;

unlink($file);

echo "Exists: ", file_exists($file) ? "yes" : "no", PHP_EOL;

How It Works

  1. file_put_contents() creates a small temporary file.
  2. file_exists() confirms that the file is there.
  3. unlink() deletes the file from the disk.
  4. The second check shows the file is gone.

Example 2 — Delete Only If the File Exists

unlink() raises a warning if the file is missing, so check with file_exists() first.

PHP
<?php
$file = __DIR__ . "/missing.txt";

if (file_exists($file)) {
    unlink($file);
    echo "Deleted.", PHP_EOL;
} else {
    echo "Nothing to delete.", PHP_EOL;
}

var_dump(@unlink($file));

How It Works

  1. The path points to a file that does not exist.
  2. file_exists() returns false, so the else branch runs.
  3. Calling unlink() on a missing file would print a warning.
  4. With the @ operator, unlink() quietly returns false instead.

Example 3 — Delete Many Temporary Files

Use glob() to find all .tmp files in a folder and delete each one.

PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    file_put_contents(__DIR__ . "/file{$i}.tmp", "data");
}

$count = 0;

foreach (glob(__DIR__ . "/*.tmp") as $file) {
    if (unlink($file)) {
        $count++;
    }
}

echo "Deleted {$count} files", PHP_EOL;

How It Works

  1. The first loop creates three demo files that end with . tmp.
  2. glob() returns the paths of all . tmp files in the folder.
  3. unlink() deletes each one, and we count the successes.
  4. The final line prints how many files were removed.

Frequently Asked Questions

No. The delete page in the PHP manual is only a pointer. Use unlink() to delete a file and unset() to remove a variable.
Call unlink($path). It returns true when the file is deleted and false when it fails.
Use rmdir() for an empty folder. If the folder has files, delete them with unlink() first, then call rmdir().
unlink() removes a file from the disk. unset() only removes a variable from memory and does not touch any file.
The file may not exist, may be open in another program, or the PHP user may not have permission to delete it from that folder.

Next: dirname()

dirname() returns the folder part of a path, which is everything before the last slash. Fo.

dirname() tutorial →

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