Deleting all files from a folder using PHP
In PHP, files from a folder can be deleted using various approaches and inbuilt methods such as unlink, DirectoryIterator and DirectoryRecursiveIterator.
Some of these approaches are explained below:
Approach 1:
- Generate a list of files using glob() method
- Iterate over the list of files.
- Check whether the name of files is valid.
- Delete the file using unlink() method.
Example:
<?php // PHP program to delete all // file from a folder // Folder path to be flushed $folder_path = "myGeeks"; // List of name of files inside // specified folder $files = glob($folder_path.'/*'); // Deleting all the files in the list foreach($files as $file) { if(is_file($file)) // Delete the given file unlink($file); } ?> |
Output:
Before Running the code:

After Running the code:

Note: Hidden files can be included in the file removal operation by addition of the below code:
$hidden_files = glob($folder_path.'/{, .}*', GLOB_BRACE); |
Approach 2:
- Generate a list of files using glob() method.
- Filter the list using array_filter() or array_merge() methods.
- Map the list to unlink() method using array_map() method.
Example:
<?php // PHP program to delete all files from a folder // Deleting all the files inside the given folder array_map('unlink', array_filter( (array) array_merge(glob("myGeeks/*")))); ?> |
Approach 3:
- Generate list of files using DirectoryIterator.
- Iterate over the list of files.
- Validate the file while checking if the file directory has a dot or not.
- Using the getPathName method reference, delete the file using unlink() method.
Example:
<?php // PHP program to delete all files // from a folder // Folder path to be flushed $folder_path = 'myGeeks/'; // Assigning files inside the directory $dir = new DirectoryIterator(dirname($folder_path)); // Deleting all the files in the list foreach ($dir as $fileinfo) { if (!$fileinfo->isDot()) { // Delete the given file unlink($fileinfo->getPathname()); } } ?> |
Approach 4:
- Generate list of all directory hierarchies inside the given folder directory using RecursiveDirectoryIterator
$dir = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); $dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
- FilesystemIterator::SKIP_DOTS is used to ignore dots while generating list of files.
- RecursiveIteratorIterator::CHILD_FIRST is used for choosing files present at root directory.
- Iterate over the file list and remove folders and files according to the specification.
Example:
<?php // PHP program to delete all FilesystemIterator // from a folder // Folder path to be flushed $dir = "myGeeks/"; // Assigning files inside the directory $dir = new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS); // Reducing file search to given root // directory only $dir = new RecursiveIteratorIterator( $dir,RecursiveIteratorIterator::CHILD_FIRST); // Removing directories and files inside // the specified folder foreach ( $dir as $file ) { $file->isDir() ? rmdir($file) : unlink($file); } ?> |
Recommended Posts:
- How to move a file into a different folder on the server using PHP?
- Find the smallest after deleting given elements
- PHP | Deleting an element from array using array_diff()
- How to include() all PHP files from a directory ?
- How to open a PDF files in web browser using PHP?
- How to load CSS files using JavaScript?
- How to count files in a directory using PHP?
- How to upload files asynchronously using jQuery?
- Perl | Reading Excel Files
- Perl | Creating Excel Files
- Creating a dataframe using Excel files
- HTML | DOM Input FileUpload files Property
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



