Found this useful code by Aidan Lister a while a go. Im posting it here for safe keeping. I might need it again in future.
/** * Delete a file, or a folder and its contents (stack algorithm) * * @author Aidan Lister <> * @version 1.0.0 * @link http://aidanlister.com/repos/v/function.rmdirr.php * @param string $dirname Directory to delete * @return bool Returns TRUE on success, FALSE on failure */ <?php function rmdirr($dirname) { // Sanity check if (!file_exists($dirname)) { echo "no directory by that name"; return false; } // Simple delete for a file if (is_file($dirname)) { return unlink($dirname); } // Loop through the folder $dir = dir($dirname); while (false !== $entry = $dir->read()) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Recurse rmdirr("$dirname/$entry"); }// end while looping // Clean up $dir->close(); return rmdir($dirname); } ?>