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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
folderName | StringScalar | Yes | — | Folder path to remove. |
flag | StringScalar | Yes | "s" | Recursive flag; only "s" is accepted. |
Returns
| Name | Type | Description |
|---|---|---|
status | NumericScalar | 1 on success, 0 when removal fails. |
msg | StringScalar | Diagnostic message for failures. |
msgID | StringScalar | Stable diagnostic identifier string. |
Returned values from rmdir depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:rmdir:NotEnoughInputs | No input arguments are provided. | rmdir: not enough input arguments |
RunMat:rmdir:TooManyInputs | More than two input arguments are provided. | rmdir: too many input arguments |
RunMat:rmdir:FolderArgType | Folder argument is not a character vector or string scalar. | rmdir: folder name must be a character vector or string scalar |
RunMat:rmdir:FlagArgType | Flag argument is not the scalar character 's'. | rmdir: flag must be the character 's' supplied as a char vector or string scalar |
RunMat:rmdir:OSError | Folder removal fails due to filesystem error. | rmdir: unable to remove folder |
RunMat:rmdir:DirectoryNotFound | Target folder does not exist. | rmdir: directory not found |
RunMat:rmdir:NotADirectory | Target path exists but is not a directory. | rmdir: target is not a directory |
RunMat:rmdir:DirectoryNotEmpty | Non-recursive removal is requested on a non-empty directory. | rmdir: directory is not empty |
RunMat:rmdir:InvalidFolderName | Folder name is empty. | Folder name must not be empty. |
How rmdir works
status = rmdir(folder)deletesfolderonly when it exists, is a directory, and is empty. Status outputs aredoublescalars (1for success,0for 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 = 0together 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.
rmdirautomatically 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 =
1Remove 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 =
1Handle A Missing Folder Gracefully
[status, message, messageID] = rmdir("not-here")Expected output:
status =
0
message =
Folder "not-here" does not exist.
messageID =
RunMat:RMDIR:DirectoryNotFoundDetect 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:DirectoryNotEmptyRemove A Directory Specified As A String Scalar On The GPU
mkdir("gpu-folder");
status = rmdir(gpuArray("gpu-folder"))Expected output:
status =
1Capture 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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how rmdir is executed, line by line, in Rust.
- View the source for rmdir in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.