View all functions

CategoryIo: Repl Fs

What does the rmdir function do in MATLAB / RunMat?

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 does the rmdir function behave in MATLAB / 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.

rmdir Function GPU Execution Behaviour

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 residency in RunMat (Do I need gpuArray?)

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 of using the rmdir function in MATLAB / RunMat

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 =
MATLAB: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 =
MATLAB: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 MATLAB: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 MATLAB: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.

See Also

mkdir, ls, dir, pwd

Source & Feedback