PHP rmdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

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.

PHP
$ok = rmdir('old-folder');
CallWhat 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
PHP
rmdir(string $directory, ?resource $context = null): bool

Parameters

ParameterDescription
directoryRequired. The path of the folder to remove. It must be empty.
contextOptional. A stream context created with stream_context_create(). Most scripts leave it out.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Create and Remove a Folder

Make a temporary folder with mkdir() and remove it with rmdir().

PHP
<?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;

How It Works

  1. mkdir() creates an empty folder and is_dir() confirms it exists.
  2. rmdir() removes the folder because there is nothing inside it.
  3. The function returns true, so Removed is printed.
  4. 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
<?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);

How It Works

  1. A folder is created and a file is placed inside it.
  2. rmdir() asks the system to remove the folder, but the system refuses.
  3. The function returns false and @ hides the PHP warning so we can print a friendly message.
  4. The last two lines clean up properly by deleting the file first and then the folder.

Example 3 — Empty a Folder, Then Remove It

Use glob() to find the files inside, delete them, and finish with rmdir().

PHP
<?php
$folder = sys_get_temp_dir() . '/rmdir_loop';
mkdir($folder);
file_put_contents($folder . '/a.tmp', '1');
file_put_contents($folder . '/b.tmp', '2');

foreach (glob($folder . '/*') as $file) {
    unlink($file);
    echo 'Deleted ', basename($file), PHP_EOL;
}

echo rmdir($folder) ? 'Folder removed' : 'Failed', PHP_EOL;

How It Works

  1. Two small files are created inside a new folder.
  2. glob() returns the full path of every item in the folder.
  3. unlink() deletes each file one at a time and basename() prints just its name.
  4. Once the folder is empty, rmdir() removes it and returns true.

Frequently Asked Questions

It removes an empty folder. It returns true when the folder is removed and false when something goes wrong.
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.
No. Delete all files with unlink() and remove sub folders first, then call rmdir() on the empty folder.
unlink() deletes a single file, while rmdir() removes an empty folder.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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