How to Delete File in PHP
In this tutorial, we will learn how to delete a file in PHP. You delete a file on your computer using Delete key or Shift + Delete. When you delete a file from your computer, the file is deleted from the current location and moved to the Recycle Bin. But when you do Shift + Delete, the file will be permanently deleted from the computer.
Similarly, managing files is one of the most common tasks on the server. Whether you’re handling user-uploaded images, temporary cache files, or log files, you’ll often need to delete files once they’re no longer needed. Deleting files is not just about freeing up storage space. It’s also about maintaining security, improving the performance of applications, and keeping your system organized.
For example, every time a user uploads a file, it stays on your server. If you don’t have the delete operation to remove it later, your server will slowly fill up with unnecessary files, leading to storage issues.
Even worse, if these files contain sensitive data, leaving unused files can create security risks. That’s why it is important to learn how to delete files in PHP is essential for everyone who wants to learn PHP programming.
PHP unlink() Function
The unlink() function in PHP is a built-in function used to delete or remove a file from the system. When you call the unlink() function to delete a file, it cuts that link and removes the file completely from your server.
Basic Syntax
Here’s the basic syntax of unlink() function in PHP:
unlink(filename, context);Parameters:
The unlink() function accepts two parameters:
- filename → This parameter specifies the path of the file you want to delete (required).
- context → This parameter defines a set of options that can change how a stream behaves. However, this is an optional parameter, so it is rarely used.
Return Value:
- The unlink() function returns TRUE if the file is successfully deleted from the system.
- Otherwise, this function returns FALSE if it is failed.
Example: Delete a File in PHP
<?php
$file = "hello.txt";
if (unlink($file)) {
echo "File '$file' deleted successfully.";
} else {
echo "Error deleting '$file'.";
}
?>Output:
File 'hello.txt' deleted successfully.
In this example, we specify the file we want to delete (“hello.txt”). We have called the unlink() function. This function returns TRUE upon successful file deletion else, FALSE on failure. It works with a 4.0 or higher version of PHP.
Uses of unlink() Function
The common scenarios where unlink() function is useful when you want to:
- Delete temporary files after processing user input.
- Remove old log files automatically to save disk space.
- Clean up unused images uploaded by users.
Example: Deletion Failed
<?php
$file = "nonexistent.txt";
if (unlink($file)) {
echo "File deleted.";
} else {
echo "Error: File does not exist or cannot be deleted.";
}
?>Output:
Error: File does not exist or cannot be deleted.
In this example, the file doesn’t exist, so PHP will throw a warning. That’s why it’s always a good practice to check if the file exists before deleting it.
Checking if a File Exists Before Deletion
If you try to delete a file blindly, it may be risky. If the file doesn’t exist, PHP will throw a warning, which isn’t good for clean and professional code. The best practice is to check if the file exists using the file_exists() function before calling unlink(). Here’s an example how you can do it.
Example:
<?php
$file = "mydatafile.txt";
if (file_exists($file)) {
if (unlink($file)) {
echo "File '$file' deleted successfully.";
} else {
echo "Error deleting '$file'.";
}
} else {
echo "File '$file' does not exist.";
}
?>Output:
File 'mydatafile.txt' does not exist.
Why check before deleting a file?
- Prevents unnecessary errors or warnings.
- Helps in debugging issues.
- Keeps your PHP script more secure and predictable.
For example, suppose you’re building an image upload system where users can delete their uploaded pictures. If a user tries to delete a picture that doesn’t exist, your script should simply tell them “File not found” instead of throwing a PHP error.
Deleting Multiple Files in PHP
Sometimes, you may need to delete more than just one file. For example, an application stored multiple temporary files after a process, and you need to clean them all up. If you delete them manually one by one, it is inefficient. Instead, you can use PHP loops to delete multiple files automatically.
Example: Deletion of Multiple Files
<?php
// An array of filenames.
$files = ["file1.txt", "file2.txt", "file3.txt"];
// Using foreach() to loop through each file.
foreach ($files as $file) {
// Check the existence of files before deletion.
if (file_exists($file)) {
// If exits, the unlink() function will remove the file.
if (unlink($file)) {
echo "Deleted: $file \n";
} else {
echo "Error deleting: $file \n";
}
} else {
echo "File not found: $file \n";
}
}
?>Output:
Deleted: file1.txt
Deleted: file2.txt
Deleted: file3.txt
Note:
1. It is important to note that unlink() function works only on the files, not directories. If you try to delete a folder using unlink() function, you’ll get an error.
2. Another important thing is permissions. If the PHP script doesn’t have permission to delete the file, the deletion will fail.

