Delete ALL Files and Subfolders from Folder in PHP

On 5/16/2017

Hi! Today let's see how to delete all files and sub-folders from a folder using php. If your website allows user uploaded contents then I bet you might want to do regular clean ups to free up more space in web server. Like deleting all files of a user without activity for long time, deleting files older than 3 months etc. Whatever the reason, doing such clean-up manually is a tedious task. You can accomplish it with the help of an automated php script. And I'm going to show you here exactly how to delete directory and all files and sub directories from it with php.

php-delete-all-files-and-subfolders-from-folder

Delete All Files from Folder in PHP:

As for deleting all files from a folder the process is pretty straight forward. You only need two native php functions glob() and unlink() to achieve this.

  • Function glob() returns an array of filenames/directories matching the given pattern.
  • Function unlink() deletes a given file.

Our process goes like this - you have to loop through the files one by one from a given folder, check if it is a file and delete file with the help of unlink() function. Here is the simple php function I have written to delete all files from a directory.

PHP Function to Delete All Files from Folder:

<?php
// delete all files from folder
function deleteFiles($dir)
{
    // loop through the files one by one
    foreach(glob($dir . '/*') as $file){
        // check if is a file and not sub-directory
        if(is_file($file)){
            // delete file
            unlink($file);
        }
    }
}
?>

Usage:

<?php deleteFiles("test"); ?>

As you can see in function deleteFiles() we have used the pattern '/*' to instruct glob() to get all the files names from the folder.

Next we loop through the files one by one and check if the item is a file and not a sub folder itself using is_file() function.

Once we confirm it as a file we delete it with unlink() method. The process will be repeated until no file is left in the specific folder.

Delete Only Particular File Type from Folder:

Say you don't want to remove all files, just the pdf files alone from a folder. Then here's the script for the task.

<?php
// delete all pdf files from folder
function deleteFiles($dir)
{
    // loop through the files one by one
    foreach(glob($dir . '/*.pdf') as $file){
        // check if is a file and not sub-directory
        if(is_file($file)){
            // delete file
            unlink($file);
        }
    }
}
?>

Delete Files Older than X Days from Folder:

This is another useful variation of the script. Suppose you want to delete older files say of at least 90 days old. You have to simply check if the last modified date and time of the file is greater than 90 days and delete if it is.

<?php
// delete all files older than 90 days
function deleteFiles($dir)
{
    // loop through the files one by one
    foreach(glob($dir . '/*') as $file){
        if(is_file($file)){
            // check if file older than 90 days
            if((time() - filemtime($file)) > (60 * 60 * 24 * 90))
                unlink($file);
        }
    }
}
?>

To check if files are older than 90 days, we have used filemtime() to get last modified timestamp of a given file, took its difference from current timestamp and checked it against 90 days count.

Delete All Files and Sub Folders from a Folder in PHP:

So far we have seen about deleting just files present in a directory. What if you have sub directories with files inside the folder. In such case the above script won't work. You'll need to create recursive function to delete all files, sub-directories and parent directory altogether.

Simply deleting a folder with rmdir() won't work. It will throw exception if you attempt to remove folder with files in it. So first you have to delete files one by one from each sub folder plus the rest of the files and then delete folders by removing parent folder.

Here is the php recursive function to delete files and sub folders from the folder.

<?php
// delete all files and sub-folders from a folder
function deleteAll($dir) {
    foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            deleteAll($file);
        else
            unlink($file);
    }
    rmdir($dir);
}
?>

Function Usage:

<?php deleteAll("test"); ?>

It will take care of deleting all files, sub folders from a folder automatically.

You can use the above php script to delete files and sub-directories in a given folder programmatically. The script is relatively fast but it depends on the amount of files and folders to be deleted. I hope you find this tutorial useful. Meet you in another interesting one :)

No comments:

Post a Comment

Contact Form

Name

Email *

Message *