Skip to main content
To recursively delete a directory and all its contents, use rm from node:fs/promises. This is like running rm -rf in JavaScript.
https://mintcdn.com/bun-1dd33a4e-claude-fix-yaml-docs/ba6flgIPltjjNPcD/icons/typescript.svg?fit=max&auto=format&n=ba6flgIPltjjNPcD&q=85&s=9c700644ac7789bb180093029561ecd4delete-directory.ts
import { rm } from "node:fs/promises";

// Delete a directory and all its contents
await rm("path/to/directory", { recursive: true, force: true });

These options configure the deletion behavior:
  • recursive: true - Delete subdirectories and their contents
  • force: true - Don’t throw errors if the directory doesn’t exist
You can also use it without force to ensure the directory exists:
https://mintcdn.com/bun-1dd33a4e-claude-fix-yaml-docs/ba6flgIPltjjNPcD/icons/typescript.svg?fit=max&auto=format&n=ba6flgIPltjjNPcD&q=85&s=9c700644ac7789bb180093029561ecd4delete-directory.ts
try {
  await rm("path/to/directory", { recursive: true });
} catch (error) {
  if (error.code === "ENOENT") {
    console.log("Directory doesn't exist");
  } else {
    throw error;
  }
}

See Docs > API > FileSystem for more filesystem operations.