Content · Programming · PHP

PHP unlink() Function: Usage and Examples

Deletes a filesystem link or file path.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP unlink() deletes a filesystem link or file path. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is unlink() in PHP?


Deletes a filesystem link or file path.

Its call structure is:

unlink($filename, [$context])

The arguments identify the values or resources passed to unlink(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.

These are examples to study, not code that the SQL import executes. Operations that affect processes, uploaded files or filesystem permissions belong in a controlled environment with validated paths and appropriate access controls.

Example 1: Basic Usage


This example introduces unlink with a direct call and a small, readable input.

<?php

$filePath = tempnam(sys_get_temp_dir(), "delete_"); if ($filePath !== false) { unlink($filePath); }

Deletes a filesystem link or file path. Avoid deleting untrusted paths; unlink() does not remove nonempty directories.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how unlink behaves.

<?php

if (is_file($temporaryPath)) { var_dump(unlink($temporaryPath)); }

The changed input illustrates why output may differ with the chosen values or environment. Avoid deleting untrusted paths; unlink() does not remove nonempty directories.

Practical Example: Using the Feature


The following example incorporates unlink into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$temporaryPath = tempnam(sys_get_temp_dir(), "upload_"); if ($temporaryPath !== false) { try { file_put_contents($temporaryPath, "temporary"); } finally { unlink($temporaryPath); } }

In a real application, check each operation result, validate values before use and clean up acquired resources. Avoid deleting untrusted paths; unlink() does not remove nonempty directories.

Important Behavior and Edge Cases


  • Purpose: Deletes a filesystem link or file path.
  • Key limitation: Avoid deleting untrusted paths; unlink() does not remove nonempty directories.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
unlink()Deletes a filesystem link or file path.
rmdir()Removes empty directories; unlink() removes a file or filesystem link.

Common Beginner Mistakes


MistakeCorrect Approach
Avoid deleting untrusted paths; unlink() does not remove nonempty directories.The correct approach depends on this behavior: Avoid deleting untrusted paths; unlink() does not remove nonempty directories.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Deletes a filesystem link or file path.
  • Avoid deleting untrusted paths; unlink() does not remove nonempty directories.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

unlink is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.