What does the delete function do in MATLAB / RunMat?
delete(filename) removes files from disk, and delete(obj) invalidates handle objects or listeners. RunMat mirrors MATLAB behaviour: it accepts character vectors, string scalars, string arrays, char matrices, and cell arrays of names for filesystem removal, gathers GPU-resident inputs automatically, and raises MATLAB-style errors when a file cannot be removed or when handle inputs are invalid.
How does the delete function behave in MATLAB / RunMat?
- Accepts individual paths, string arrays, cell arrays of character vectors, and char matrices. Each element targets one file.
- Accepts handle objects (
handle) and event listeners, marking them invalid (isvalidreturnsfalse) without touching the filesystem when invoked with non-string inputs. - Expands shell-style wildcards (
*and?) using MATLAB-compatible rules. Patterns must resolve to existing files; otherwise, the builtin throwsMATLAB:DELETE:FileNotFound. - Rejects folders. When a target is a directory, RunMat raises
MATLAB:DELETE:Directories, matching MATLAB’s “Use rmdir to remove directories” diagnostic. - Propagates operating-system failures (for example, permission errors or read-only files) through
MATLAB:DELETE:PermissionDenied. - Expands
~to the user’s home directory and resolves relative paths against the current working folder (pwd). - Treats empty character vectors or empty string scalars as invalid inputs and raises
MATLAB:DELETE:EmptyFilename. - When passed an empty string array or empty cell array, the builtin performs no action and returns without error, just like MATLAB.
delete Function GPU Execution Behaviour
delete performs host-side filesystem I/O. When a path argument lives on the GPU (for example, gpuArray("scratch.log")), RunMat gathers the scalar to CPU memory before touching the filesystem. Acceleration providers do not implement dedicated hooks for delete, so there are no GPU kernels or device transfers beyond the automatic gathering of inputs.
Examples of using the delete function in MATLAB / RunMat
Deleting a single temporary file
fname = "scratch.txt";
fid = fopen(fname, "w");
fclose(fid);
delete(fname);
Removing multiple files with a wildcard pattern
logs = ["log-01.txt", "log-02.txt"];
for name = logs
fid = fopen(name, "w");
fclose(fid);
end
delete("log-*.txt");
Deleting files listed in a string array
files = ["stageA.dat", "stageB.dat"];
for name = files
fid = fopen(name, "w");
fclose(fid);
end
delete(files);
Handling missing files safely with try/catch
try
delete("missing-file.txt");
catch err
disp(err.identifier)
disp(err.message)
end
Cleaning up build artifacts stored under your home folder
delete("~/runmat/build/*.o");
Deleting char-matrix filenames generated programmatically
names = char("stage1.tmp", "stage2.tmp");
for row = 1:size(names, 1)
fname = strtrim(names(row, :));
fid = fopen(fname, "w");
fclose(fid);
end
delete(names);
Deleting graphics handles after use
fig = figure;
delete(fig);
tf = isvalid(fig);
Expected output:
tf =
0
GPU residency in RunMat (Do I need gpuArray?)
No. delete executes on the CPU. If a script accidentally wraps path strings in gpuArray, RunMat gathers the scalars before issuing filesystem calls. Keeping paths on the GPU provides no benefit.
FAQ
- What message IDs does
deleteproduce? Missing files raiseMATLAB:DELETE:FileNotFound, directories raiseMATLAB:DELETE:Directories, wildcard syntax issues raiseMATLAB:DELETE:InvalidPattern, operating-system failures raiseMATLAB:DELETE:PermissionDenied, and invalid handle inputs raiseMATLAB:DELETE:InvalidHandle. - Can I delete folders with
delete? No. MATLAB reserves folder deletion forrmdir. RunMat follows suit and throwsMATLAB:DELETE:Directorieswhen a target is a directory. - Does
deletesupport multiple filenames at once? Yes. Pass a string array, a cell array of character vectors, or a char matrix. Each element is deleted in turn. - How are wildcard patterns resolved? RunMat uses MATLAB-compatible globbing:
*matches any sequence,?matches a single character, and the pattern is evaluated relative to the current folder (pwd) unless you pass an absolute path. - What happens when a wildcard matches nothing? The builtin raises
MATLAB:DELETE:FileNotFound(just like MATLAB) and leaves the filesystem unchanged. - Do empty arrays raise errors? Empty string arrays or empty cell arrays simply result in no deletions. Empty strings, however, are invalid and trigger
MATLAB:DELETE:EmptyFilename. - How do GPU inputs behave? Inputs on the GPU are gathered to the host automatically. No GPU kernels are launched.
- Does
deletepreserve symbolic links? Yes. RunMat delegates to the operating system: deleting a symlink removes the link itself, not the target—matching MATLAB. - Can I detect failures programmatically? Wrap the call in
try/catchand inspecterr.identifieranderr.messagejust as you would in MATLAB. - Will
deletefollow relative paths updated bycd? Yes. Paths are interpreted using the process working directory, so callingcdbeforedeletemirrors MATLAB’s behaviour.
See Also
copyfile, movefile, rmdir, dir, pwd
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/repl_fs/delete.rs - Issues: Open a GitHub ticket with a minimal reproduction.