RunMat
GitHub

rmdir — Remove folders with MATLAB-compatible status, message, and message ID outputs.

rmdir removes folders from the filesystem. Like MATLAB, it can optionally delete the folder's contents when you pass the 's' flag and returns diagnostic status information instead of throwing exceptions.

How rmdir works in RunMat

  • status = rmdir(folder) deletes folder only when it exists, is a directory, and is empty. Status outputs are double scalars (1 for success, 0 for failure).
  • [status, message, messageID] = rmdir(folder) returns the diagnostic message and MATLAB-style message identifier. Successful deletions populate both outputs with empty character arrays (1×0).
  • rmdir(folder, 's') removes the folder and all descendants recursively. The flag is case-insensitive and accepts either a char vector or a string scalar with the text "s".
  • Passing a path that does not exist or that resolves to a file returns status = 0 together with descriptive diagnostics. Paths are resolved relative to the current working directory and expand a leading ~ into the user's home directory.
  • Invalid inputs—including numeric arrays, logical values, multi-element string arrays, or empty character vectors—raise MATLAB-style errors before any filesystem work occurs.
  • rmdir automatically gathers GPU-resident path strings (for example, gpuArray("logs")) to host memory before evaluating the filesystem operation. There is no device-side acceleration.

How rmdir runs on the GPU

rmdir performs host-side filesystem operations. When acceleration providers are active, RunMat first gathers any GPU-resident arguments, performs the removal on the CPU, and returns host-resident outputs. Providers do not expose special hooks for this builtin, so GPU execution is not applicable.

GPU memory and residency

No. rmdir always executes on the host CPU, therefore placing arguments on the GPU offers no benefit. If a path value is already wrapped in gpuArray, RunMat gathers it automatically before accessing the filesystem so existing scripts continue to work unchanged.

Examples

Remove An Empty Folder

mkdir("scratch");
status = rmdir("scratch")

Expected output:

status =
     1

Remove A Folder And Its Contents

mkdir("data/project");
fid = fopen(fullfile("data/project", "notes.txt"), "w");
fprintf(fid, "Draft notes");
fclose(fid);
status = rmdir("data", "s")

Expected output:

status =
     1

Handle A Missing Folder Gracefully

[status, message, messageID] = rmdir("not-here")

Expected output:

status =
     0
message =
Folder "not-here" does not exist.
messageID =
RunMat:RMDIR:DirectoryNotFound

Detect That A Folder Is Not Empty Without The 's' Flag

mkdir("logs");
fid = fopen(fullfile("logs", "today.log"), "w");
fprintf(fid, "Log entry");
fclose(fid);
[status, message, messageID] = rmdir("logs")

Expected output:

status =
     0
message =
Cannot remove folder "logs": directory is not empty.
messageID =
RunMat:RMDIR:DirectoryNotEmpty

Remove A Directory Specified As A String Scalar On The GPU

mkdir("gpu-folder");
status = rmdir(gpuArray("gpu-folder"))

Expected output:

status =
     1

Capture Status And Message Outputs

mkdir("reports");
[status, message, messageID] = rmdir("reports")

Expected output:

status =
     1
message =

messageID =

FAQ

What status codes does rmdir return?

1 means the folder was removed (or already absent after a recursive call), while 0 means the folder could not be removed.

Does rmdir throw exceptions?

Only invalid inputs raise errors. Filesystem failures surface through the status, message, and message ID outputs.

How do I remove non-empty folders?

Pass the 's' flag (for example, rmdir("folder", "s")). Without it, rmdir refuses to delete directories that contain files or subfolders.

Is the 's' flag case-sensitive?

No. Use 's' or 'S', supplied as either a char vector or a string scalar with a single element.

Can I target files instead of folders?

No. Passing a file path returns status = 0 with the message ID RunMat:RMDIR:NotADirectory.

How are paths resolved?

Relative paths are resolved against the current working folder (pwd). Leading ~ segments expand to the user's home directory on each platform.

Does rmdir require GPU acceleration?

No. The builtin executes on the host. If an argument resides on the GPU, RunMat gathers it automatically before touching the filesystem.

What happens if the folder is already missing when I pass 's'?

The function reports status = 0 and the message ID RunMat:RMDIR:DirectoryNotFound; it never creates folders.

Can I remove folders using UNC paths or drive letters on Windows?

Yes. The builtin forwards the path to the operating system exactly as MATLAB does.

Why are the message outputs character arrays instead of strings?

MATLAB returns char arrays for compatibility. Wrap them with string(message) if you prefer string scalars.

These functions work well alongside rmdir. Each page has runnable examples you can try in the browser.

mkdir, ls, dir, pwd, addpath, cd, copyfile, delete, exist, fullfile, genpath, getenv, movefile, path, rmpath, savepath, setenv, tempdir, tempname

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how rmdir works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.