Deleting all files from a folder using PHP Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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 // 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: php $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 // 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 // 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 // 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); } ?> Comment H Harshit Saini Follow 1 Improve H Harshit Saini Follow 1 Improve Article Tags : Web Technologies PHP PHP Programs PHP-file-handling Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like