Skip to content

Instantly share code, notes, and snippets.

@jleehr
Created October 9, 2022 10:26
Show Gist options
  • Save jleehr/809d0d7b1c00fb9c40deb78c949ffd8f to your computer and use it in GitHub Desktop.
Save jleehr/809d0d7b1c00fb9c40deb78c949ffd8f to your computer and use it in GitHub Desktop.
Delete directory and files recursive
<?php
$dir = __DIR__ . '/data/folder-to-delete';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment