Remove all files and subfolders from a folder in PHP
Today we’ll let you know how to remove all files and subfolders from a folder in PHP. We have already written an article to delete all files from a folder in PHP.
As per the request of our readers, we decided to provide the solution to their query for recursively delete a directory and its entire contents using PHP.
Delete All Files and Sub Folders from a Folder in PHP, Remove all files, folders and their subfolders with php, How to recursively delete a directory and its entire contents, Recursively Remove a Directory in PHP, php code to delete folder with files, php delete files matching pattern, php list files in directory and subdirectories, how to delete images from folder in php
Checkout more articles on PHP
Steps to remove all files from a folder and their subdirectories
1. Function to delete all files
Here we write a function to delete all files and subfolders from the folder.
delete-all-files.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php // function to delete all files and subfolders from folder function deleteAll($dir, $remove = false) { $structure = glob(rtrim($dir, "/").'/*'); if (is_array($structure)) { foreach($structure as $file) { if (is_dir($file)) deleteAll($file,true); else if(is_file($file)) unlink($file); } } if($remove) rmdir($dir); } // folder path that contains files and subfolders $path = "./uploads"; // call the function deleteAll($path); echo "All files and subfolders deleted successfully."; exit; ?> |
In the above function, we passed the two parameters where the first parameter indicates the path of the main folder and second one is an optional parameter that we set default false
. If it is false then remove the folder and files otherwise only files will be removed.
Here we used the rmdir
function to delete the folder that we can not execute before making it empty.
2. Execute function
Let’s create a simple page that helps us to execute the above function on click of a tag.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html> <html> <head> <title> Remove all files from a folder and their subdirectories in PHP - Clue Mediator </title> </head> <body style="padding: 20px; font-family: arial;"> <h2> Remove all files - <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a> </h2> <div style="line-height: 25px;"> <a href="./delete-all-files.php" title="Delete all files">Delete all files</a> </div> </body> </html> |
3. Output
Run the code and check the output.
That’s it for today.
Thank you for reading. Happy Coding!
Thank you, very useful.
Is it possible to make it delete contents that are older than X time! let say older than 24 hours?
Thank you
Hi Amir,
Check the following link. Hope it will help you!
Delete files older than x days or after x amount of time in PHP
sorry something went wrong when in the copy and paste!! please delete that, if you interested i can email it to you.
kind regards
Hi Amir, You can share your query by email. We’ll try our best.