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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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); 
}
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

$hidden_files = glob($folder_path.'/{, .}*', GLOB_BRACE);

chevron_right


Approach 2:



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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/*"))));
  
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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());
    }
}
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
}
?>

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.