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");
Call
What 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
Parameter
Description
filename
Required. The path of the file to delete. This is the argument that unlink() takes.
context
Optional. A stream context, supported by unlink() only. Rarely needed.
Return Value
Return Value
Description
bool
unlink() 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.