rmdir
rmdir(string $directory, ?resource $context = null): bool
Removes an empty directory.
C C23
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void) {
mkdir("demo", 0755);
FILE *fp = fopen("demo/note.txt", "w");
fputs("x", fp);
fclose(fp);
printf("%d\n", rmdir("demo") == 0); /* POSIX rmdir fails on non-empty directories (returns -1) */
unlink("demo/note.txt"); /* empty the directory first */
printf("%d\n", rmdir("demo") == 0); /* only empty directories can be removed */
return 0;
}
0 1
C++ C++26
#include <filesystem>
#include <fstream>
#include <iostream>
int main() {
std::filesystem::create_directory("demo");
std::ofstream("demo/note.txt") << "x";
std::error_code ec;
std::filesystem::remove("demo", ec); // remove = unlink/rmdir in one; the ec overload reports errors instead of throwing
std::cout << (ec ? 0 : 1) << "\n";
std::filesystem::remove("demo/note.txt", ec); // empty the directory first
std::filesystem::remove("demo", ec); // now it can be removed
std::cout << (std::filesystem::exists("demo") ? 0 : 1) << "\n";
return 0;
}
0 1
C# v11.0.100
var dir = Directory.CreateDirectory("demo");
File.WriteAllText("demo/note.txt", "x");
bool removed;
try {
dir.Delete(); // removing a non-empty directory throws IOException directly
removed = true;
} catch (IOException) {
removed = false;
}
Console.WriteLine(removed);
File.Delete("demo/note.txt"); // empty the directory first
dir.Delete(); // only empty directories can be removed; the Delete(true) overload removes recursively
Console.WriteLine(Directory.Exists("demo"));
False False
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
os.Mkdir("demo", 0755)
os.WriteFile("demo/note.txt", []byte("x"), 0644)
if err := os.Remove("demo"); err != nil { // Remove is unlink/rmdir in one: empty dir is removed as a directory, non-empty errors out
fmt.Println("fail")
}
os.Remove("demo/note.txt") // empty the directory first
os.Remove("demo") // now it can be removed
_, err := os.Stat("demo")
fmt.Println(err != nil) // directory no longer exists
}
fail true
Java v27.0.1
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File dir = new File("demo");
dir.mkdir();
new File("demo/note.txt").createNewFile();
System.out.println(dir.delete()); // File.delete: removing a non-empty directory fails and returns false
new File("demo/note.txt").delete(); // empty the directory first
System.out.println(dir.delete()); // only empty directories can be removed
System.out.println(dir.exists());
}
}
false true false
JavaScript v26.13.1
const fs = require("node:fs");
fs.mkdirSync("demo");
fs.writeFileSync("demo/note.txt", "x");
try {
fs.rmdirSync("demo"); // throws on a non-empty directory
} catch {
console.log("fail");
}
fs.unlinkSync("demo/note.txt"); // empty the directory first
fs.rmdirSync("demo"); // only empty directories can be removed
console.log(fs.existsSync("demo"));
fail false
PHP v8.6.0
mkdir("demo");
file_put_contents("demo/note.txt", "x");
var_dump(@rmdir("demo")); // non-empty directory fails and returns false (@ suppresses the warning)
unlink("demo/note.txt"); // empty the directory first
var_dump(rmdir("demo")); // only empty directories can be removed
var_dump(is_dir("demo")); // directory no longer exists
bool(false) bool(true) bool(false)
Python v3.15.2
import os
os.mkdir("demo")
with open("demo/note.txt", "w") as f:
f.write("x")
try:
os.rmdir("demo") # raises OSError on a non-empty directory
except OSError:
print("fail")
os.remove("demo/note.txt") # empty the directory first
os.rmdir("demo") # only empty directories can be removed
print(os.path.exists("demo"))
fail False
Rust v1.99.1
use std::fs;
use std::path::Path;
fn main() {
fs::create_dir("demo").unwrap();
fs::write("demo/note.txt", "x").unwrap();
match fs::remove_dir("demo") { // remove_dir removes only empty directories; non-empty is an error
Ok(()) => println!("true"),
Err(_) => println!("false"),
}
fs::remove_file("demo/note.txt").unwrap(); // empty the directory first
fs::remove_dir("demo").unwrap(); // now it can be removed
println!("{}", !Path::new("demo").exists()); // directory no longer exists
}
false true
TypeScript v26.13.1
import * as fs from "node:fs";
fs.mkdirSync("demo");
fs.writeFileSync("demo/note.txt", "x");
try {
fs.rmdirSync("demo");
} catch {
console.log("fail");
}
fs.unlinkSync("demo/note.txt");
fs.rmdirSync("demo");
console.log(fs.existsSync("demo"));
fail false
Language differences
C: rmdir reports failure via return value / error code.
C++: remove(path, &ec) reports via error code; whole-tree removal with std::filesystem::remove_all.
C#: DirectoryInfo.Delete() throws IOException for non-empty directories; whole-tree removal with Delete(true).
Go: os.Remove (unlink/rmdir in one) reports via error code.
Java: File.delete() returns false without throwing; no single-call whole-tree API — the idiom is walking with Files.walk in reverse order and deleting one by one.
JavaScript: fs.rmdirSync throws; for whole-tree removal prefer fs.rmSync(dir, { recursive: true }) (rmdirSync's recursive option is deprecated).
PHP: the shared constraint — only empty directories can go; rmdir returns false and raises a warning on failure (the example suppresses it with @); emptying the directory first (the unlink step in the examples) is the universal cross-language flow.
Python: os.rmdir throws; whole-tree removal with shutil.rmtree.
Rust: whole-tree removal with fs::remove_dir_all.
TypeScript: same as JS — fs.rmdirSync.