Home PHP Filesystem rmdir() PHP rmdir() Function Definition In PHP, rmdir() removes an empty folder from the file system.
It is the opposite of mkdir(). The folder must be completely empty first. If it still contains files or other folders, rmdir() fails, so you delete the contents before removing the folder.
Syntax Call rmdir() with the values you need. Start with this line.
$ok = rmdir('old-folder'); Call What it does rmdir('temp')Removes the folder temp if it is empty. rmdir(__DIR__ . '/cache')Removes a folder using a full path. unlink('temp/a.txt'); rmdir('temp')Deletes the file first, then removes the now empty folder.
rmdir() cannot delete a folder that has files in it. To remove a whole folder tree you need to loop through the contents, delete each file with unlink(), and remove sub folders with rmdir() one by one.
Library declarations rmdir(string $directory, ?resource $context = null): bool Parameters Parameter Description directory Required. The path of the folder to remove. It must be empty. context Optional. A stream context created with stream_context_create(). Most scripts leave it out.
Return Value Return Value Description bool Returns true on success or false on failure.
PHP Examples 1. Create and Remove a Folder 2. A Folder That Is Not Empty 3. Empty a Folder, Then Remove It Example 1 — Create and Remove a Folder Make a temporary folder with mkdir() and remove it with rmdir().
<?php
$folder = sys_get_temp_dir() . '/rmdir_demo';
mkdir($folder);
echo 'Created: ', is_dir($folder) ? 'yes' : 'no', PHP_EOL;
if (rmdir($folder)) {
echo 'Removed', PHP_EOL;
}
echo 'Still exists: ', is_dir($folder) ? 'yes' : 'no', PHP_EOL; Created: yes
Removed
Still exists: no How It Works mkdir() creates an empty folder and is_dir() confirms it exists. rmdir() removes the folder because there is nothing inside it. The function returns true, so Removed is printed. is_dir() now returns false, which proves the folder is gone. Example 2 — A Folder That Is Not Empty rmdir() fails when the folder still holds a file. Check the result and explain why.
<?php
$folder = sys_get_temp_dir() . '/rmdir_full';
mkdir($folder);
file_put_contents($folder . '/note.txt', 'keep me');
if (@rmdir($folder)) {
echo 'Removed', PHP_EOL;
} else {
echo 'Cannot remove: folder is not empty', PHP_EOL;
}
unlink($folder . '/note.txt');
rmdir($folder); Cannot remove: folder is not empty How It Works A folder is created and a file is placed inside it. rmdir() asks the system to remove the folder, but the system refuses. The function returns false and @ hides the PHP warning so we can print a friendly message. The last two lines clean up properly by deleting the file first and then the folder. Frequently Asked Questions What does rmdir() do in PHP? It removes an empty folder. It returns true when the folder is removed and false when something goes wrong.
Why does rmdir() fail? The most common reason is that the folder is not empty. Other reasons are a wrong path, a missing folder, or no permission to delete it.
Can rmdir() delete a folder with files inside? No. Delete all files with unlink() and remove sub folders first, then call rmdir() on the empty folder.
What is the difference between rmdir() and unlink()? unlink() deletes a single file, while rmdir() removes an empty folder.
How do I delete a folder and everything in it? Write a loop or a small recursive function that deletes files with unlink() and sub folders with rmdir(), then remove the main folder last.
Next: set_file_buffer() set_file_buffer() sets the write buffer size for a file stream. It is an alias of stream_s.
set_file_buffer() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion