RunMat
GitHub

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

rmdir removes folders from the filesystem. It follows MATLAB-compatible forms for non-recursive and recursive deletion ('s') and returns status/message/messageID outputs for operational diagnostics.

Syntax

status = rmdir(folderName)
status = rmdir(folderName, flag)
[status, msg, msgID] = rmdir(folderName)
[status, msg, msgID] = rmdir(folderName, flag)

Inputs

NameTypeRequiredDefaultDescription
folderNameStringScalarYesFolder path to remove.
flagStringScalarYes"s"Recursive flag; only "s" is accepted.

Returns

NameTypeDescription
statusNumericScalar1 on success, 0 when removal fails.
msgStringScalarDiagnostic message for failures.
msgIDStringScalarStable diagnostic identifier string.

Returned values from rmdir depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:rmdir:NotEnoughInputsNo input arguments are provided.rmdir: not enough input arguments
RunMat:rmdir:TooManyInputsMore than two input arguments are provided.rmdir: too many input arguments
RunMat:rmdir:FolderArgTypeFolder argument is not a character vector or string scalar.rmdir: folder name must be a character vector or string scalar

How rmdir works

  • 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.

Does RunMat run rmdir 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 =

Using rmdir with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how rmdir changes the result.

Run a small rmdir example, explain the result, then change one input and compare the output.

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.

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.