php code to delete all file and folder at path

⚠️ Important Notes

  • This does NOT delete the main /tmp/ folder, only its contents.
  • Make sure:
    • You have proper permissions (chmod if needed)
    • Path is 100% correct
  • Once deleted, data cannot be recovered
<?php

function deleteAll($dir) {
    if (!is_dir($dir)) {
        return;
    }

    $files = scandir($dir);

    foreach ($files as $file) {
        if ($file != "." && $file != "..") {
            $fullPath = $dir . '/' . $file;

            if (is_dir($fullPath)) {
                deleteAll($fullPath); // Recursive call
                rmdir($fullPath);     // Remove empty folder
            } else {
                unlink($fullPath);    // Delete file
            }
        }
    }
}

// Target directory
$targetDir = '/home/uhmn6p9kb8ir/tmp/';

// Run delete
deleteAll($targetDir);

echo "All files and folders deleted successfully.";

?>

Safer Version (with confirmation check)

If you want to prevent accidental execution:

if (isset($_GET['confirm']) && $_GET['confirm'] === 'yes') {
    deleteAll('/home/uhmn6p9kb8ir/tmp/');
    echo "Deleted!";
} else {
    echo "Add ?confirm=yes to URL to execute.";
}