Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Recursively Delete a Directory and its Entire Contents (files + sub dirs) in PHP
In PHP, recursively deleting a directory and its entire contents requires careful handling of files and subdirectories. PHP provides several approaches to accomplish this task using built-in functions and classes.
Method 1: Using rmdir() and unlink() Functions
This method uses scandir() to list directory contents, then recursively processes each item
<?php
function deleteDirectory($dirPath) {
if (is_dir($dirPath)) {
$files = scandir($dirPath);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$filePath = $dirPath . '/' . $file;
if (is_dir($filePath)) {
deleteDirectory($filePath);
} else {
unlink($filePath);
}
}
}
rmdir($dirPath);
}
}
// Usage
$directoryPath = '/path/to/directory';
deleteDirectory($directoryPath);
?>
This approach first checks if the path is a directory, scans its contents, and recursively deletes subdirectories while unlinking files directly.
Method 2: Using glob() Function
The glob() function provides a cleaner way to retrieve directory contents using pattern matching
<?php
function deleteDirectory($dirPath) {
$files = glob($dirPath . '/*');
foreach ($files as $file) {
if (is_dir($file)) {
deleteDirectory($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
// Usage
$directoryPath = '/path/to/directory';
deleteDirectory($directoryPath);
?>
This method uses glob() with the /* pattern to get all files and directories, eliminating the need to filter out . and .. entries.
Method 3: Using RecursiveIteratorIterator
The most sophisticated approach uses PHP's iterator classes for efficient traversal
<?php
function deleteDirectory($dirPath) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($dirPath);
}
// Usage
$directoryPath = '/path/to/directory';
deleteDirectory($directoryPath);
?>
The CHILD_FIRST flag ensures child elements are processed before parents, while SKIP_DOTS ignores . and .. entries.
Comparison
| Method | Performance | Code Complexity | Memory Usage |
|---|---|---|---|
| scandir() | Good | Simple | Low |
| glob() | Good | Simpler | Low |
| RecursiveIterator | Excellent | Moderate | Very Low |
Important: Ensure proper file permissions before attempting to delete directories. These operations are irreversible and require appropriate access rights.
Conclusion
All three methods effectively delete directories recursively. Use glob() for simplicity, scandir() for maximum compatibility, or RecursiveIteratorIterator for performance with large directory structures.
