Skip to content
PHP

Recursively Remove a Directory in PHP

1 min read

A simple method for recursively deleting a directory and all its content in PHP:-

function removeDirectory($path) {

	$files = glob($path . '/*');
	foreach ($files as $file) {
		is_dir($file) ? removeDirectory($file) : unlink($file);
	}
	rmdir($path);

	return;
}

To use simply pass the path to the directory you want to remove:-

removeDirectory('/path/to/directory');
© 2024 Andy Carter