PHP Remove Directory and Files in Directory Example

By Hardik Savani | July 31, 2020 | Category : PHP

This tutorial will give you example of php delete directory and files in directory. you can understand a concept of php remove folder and all files. This post will give you simple example of php remove directory and all files. let’s discuss about how to delete directory with files in php.

Sometime, we need to delete all files and directory in a directory using php code. so in this post i will help you how to remove folder with all contents inside that folder.

Let's see both example that will helps you.

Example 1:



$folderName = 'images';


function removeFolder($folderName) {

if (is_dir($folderName))

$folderHandle = opendir($folderName);

if (!$folderHandle)

return false;

while($file = readdir($folderHandle)) {

if ($file != "." && $file != "..") {

if (!is_dir($folderName."/".$file))








return true;



Example 2:



$folderName = 'images2';


function removeFiles($target) {


$files = glob( $target . '*', GLOB_MARK );

foreach( $files as $file ){

removeFiles( $file );


rmdir( $target );

} elseif(is_file($target)) {

unlink( $target );




I hope it can help you...

Tags : PHP

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: