Delete directory recursively using PHP

In this PHP tutorial, we will learn how to use PHP to delete a directory and any files which may be contained in the directory. The rmdir command is used to tell PHP to remove a directory, but this only works on empty directories. If we want to remove a directory which contains subdirectories (or subfolders if you like) and files in these subdirectories we need to write our own function which gets a bit more involved.

To solve the problem, we will need to write a function which works recursively to delete each subfolder (in the original directory) and any files or subfolders which may be present in those subfolders.

PHP function to delete a directory and files recursively

Take a look at the following code:

<?php
 
// simply pass the directory to be deleted to this function
 
function rrmdir($dir)
{
    if (is_dir($dir)) // ensures that we actually have a directory
    {
        $objects = scandir($dir); // gets all files and folders inside
        foreach ($objects as $object)
        {
            if ($object != '.' && $object != '..')
            {
                if (is_dir($dir . '/' . $object))
                {
                    // if we find a directory, do a recursive call
                    rrmdir($dir . '/' . $object);
                }
                else
                {
                    // if we find a file, simply delete it
                    unlink($dir . '/' . $object);
                }
            }
        }
        // the original directory is now empty, so delete it
        rmdir($dir);
    }
}
 
?>

We simply pass the directory to be deleted as a string to the function above.

The function first checks that an actual directory has been passed to it. It then grabs all the files and folders contained in the directory and stores it in an array. It then goes through these objects one by one and processes them.

Note that the function ignores "." and ".." which are symbols for the current directory and directory above the current directory respectively.

The function then checks each object to see if it is a file or directory. If it is a file, it simply deletes it. If it is another directory, a recursive call is made to delete any further subdirectories and files which may be contained within it.

Things to note

This function can cause a lot of damage if used incorrectly so take care when using it. It is always a good idea to make backups before running any code which will alter data.

Note that for this function to work properly, the user which PHP runs as (usually the web server) must have the appropriate permissions to delete all files and folders which may be contained in the directory as well as the directory itself. If you encounter any problems using this code, chances are it is a permissions problem.

If you are having issues, take a look at any files or folders which aren't being deleted and check that the web server has the appropriate permissions to modify them.

We hope you found this PHP tutorial useful.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...