Welcome to a tutorial on how to delete files in PHP. Need to remove some old, backup, or temporary files in PHP?
- To delete a file in PHP, use
unlink("TARGET FILE");
- To remove an empty folder, use
rmdir("FOLDER");
That covers the super quick basics, but “advanced deleting” entire folders and only certain file types are a different story. Just how do we do those? Read on for more examples!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
WAYS TO DELETE FILES IN PHP
All right, let us now get into the examples of how to delete files in PHP.
1) SIMPLE DELETE
<?php
// (A) DELETE FILE
echo unlink("file.txt") ? "OK" : "ERROR" ;
// (B) DELETE FOLDER
echo rmdir("d:/test") ? "OK" : "ERROR" ;
As in the introduction above, we use unlink()
to delete files, and rmdir()
to remove directories. Take note that both functions will actually return true/false
to indicate if the process is successful; Copy-protected files cannot be deleted, and folders that are not empty cannot be removed.
2) DELETE IF FILE EXISTS
<?php
// (A) CHECK BEFORE DELETE
function checkdel ($file) {
if (!file_exists($file)) { return false; }
return unlink($file);
}
// (B) GO!
echo checkdel("file.txt") ? "OK" : "ERROR" ;
PHP does throw annoying warnings when deleting files that do not exist. So here is a “quick fix” – Just do a file_exists()
check before proceeding with the delete.
3) DELETE ONLY CERTAIN FILE TYPES
<?php
// (A) DELETE ONLY CERTAIN FILE TYPES
function deltype ($ext, $folder) {
// (A1) GET ALL FILES OF SPECIFIED EXTENSION
$files = glob("$folder*.{$ext}", GLOB_BRACE);
// (A2) LOOP AND DELETE
if (count($files)>0) { foreach ($files as $f) {
echo unlink($f)
? "$f deleted\r\n"
: "Error deleting $f\r\n" ;
}}
}
// (B) GO!
deltype("jpg,png,gif", "d:/test/");
Only want to clear out certain file types in a folder? Use the glob()
function to help filter out a list of the selected files, then delete them one by one.
4) DELETE FILES ONLY IF OLDER THAN A SET DATE
<?php
// (A) DELETE ONLY IF OLDER THAN SET DATE
function delold ($keepDate, $folder) {
// (A1) GET ALL FILES INSIDE FOLDER
$files = glob("$folder*", GLOB_BRACE);
// (A2) LOOP, CHECK LAST MODIFIED, DELETE
if (count($files)>0) {
$keepDate = strtotime($keepDate);
foreach ($files as $f) { if (filemtime($f) < $keepDate) {
echo unlink($f)
? "$f deleted\r\n"
: "Error deleting $f\r\n" ;
}}
}
}
// (B) GO!
// DATE FORMAT YYYY-MM-DD... OR ANYTHING THAT STRTOTIME CAN UNDERSTAND
delold("2020-11-10", "d:/test/");
Want to remove some old backup or outdated files? It’s pretty much the same story of using good old glob()
to get the list of files in the folder. Then using filemtime()
to check against the “file last modified” time – Delete the file if it is older than a set date.
5) DELETE ENTIRE FOLDER
<?php
function delFolder ($folder) {
// (A1) GET ALL FILES + FOLDERS
$all = glob("$folder*", GLOB_BRACE);
if (count($all)>0) { foreach ($all as $a) {
// (A2) FOLDER - RECURSIVE DELETE
if (is_dir($a)) { delFolder("$a/"); }
// (A3) DELETE FILES
else {
echo unlink($a)
? "$a deleted\r\n"
: "Error deleting $a\r\n" ;
}
}}
// (A4) DELETE CURRENT FOLDER ITSELF
echo rmdir($folder)
? "$folder deleted\r\n"
: "Error deleting $folder\r\n" ;
}
// (B) GO!
delFolder("d:/test/");
One of the more confusing ones, since we can’t just use rmdir()
to delete the entire folder directly. Not going to explain this line-by-line, but in essence, delFolder()
is a recursive function that will dig into a given folder and delete everything inside.
6) COMMAND LINE DELETE
<?php
// (A) WINDOWS
echo exec('del "d:\\test\\test.txt"');
// (B) LINUX
echo exec('rm /path/file/test.txt');
Finally, this is not quite “delete in PHP”… But we can actually run shell commands in PHP to delete files and folders.
USEFUL BITS & LINKS
That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.
THE SUMMARY
Function | Description | Reference Link |
unlink(TARGET) |
Delete target file. | Click Here |
rmdir(FOLDER) |
Remove a folder. | Click Here |
file_exists(FILE) |
Checks if the target file exists. | Click Here |
glob(PATTERN) |
Get all pathnames for the given pattern/target folder. | Click Here |
exec() |
Run a shell command. | Click Here |
MORE LINKS
- How To Read Files In PHP – Code Boxx
- How To Write Files In PHP – Code Boxx
- Absolute & Relative Path – Code Boxx
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!