delete — Remove files or invalidate handles.
delete(filename) removes files without a MATLAB-facing output; delete(obj) invalidates a handle or listener. The ResolveSymbolicLinks control selects link or target deletion.
Syntax
delete(filename)
delete(filename1, filename2, ...)
delete(___, ResolveSymbolicLinks=tf)Inputs
filename- Type
Any- Required
- Yes
- Description
- Filename/pattern string, string array, char matrix, cell array of path strings, or handle input.
filename1- Type
Any- Required
- Yes
- Description
- First filename/pattern/handle input.
filenameN- Type
Any- Required
- Variadic
- Description
- Additional filename/pattern/handle inputs.
filename- Type
Any- Required
- Yes
- Description
- Filename, filename collection, or wildcard pattern.
ResolveSymbolicLinks- Type
StringScalar- Required
- Yes
- Description
- Name of the symbolic-link resolution option.
tf- Type
Any- Required
- Yes
- Default
false- Description
- Scalar logical or numeric zero/one selecting link-target deletion.
Errors
RunMat:delete:InvalidInput- When
- Input arguments are missing or contain unsupported filename value types.
- Message
delete: invalid input
RunMat:delete:InvalidHandle- When
- Handle deletion inputs are mixed with filename inputs or contain unsupported handle values.
- Message
delete: invalid handle input
RunMat:delete:EmptyFilename- When
- A filename input is empty after trimming.
- Message
delete: filename cannot be empty
Files and patterns
delete(filename) removes one file without a MATLAB-facing output. Filenames may be text scalars, string arrays, character matrices, or cells of path text. Each target is processed in order; a later failure does not restore earlier deletions.
Patterns accept * and ? and must match existing files. Directories are rejected; use rmdir for a folder. Direct paths and wildcard enumeration use the same captured filesystem provider and working directory. A leading ~ expands to the user's home directory; for example, delete('~/runmat/build/*.o') cleans matching build artifacts when that directory exists.
An empty string scalar or empty character row is invalid. Empty string arrays and cells perform no deletions.
Links and handles
delete(filename, 'ResolveSymbolicLinks', tf) accepts logical tf or exact numeric zero/one, including every built-in integer class. False removes the link itself; true removes its resolved file or empty-folder target and leaves the link.
delete(obj) invalidates handle objects and event listeners without filesystem access. A handle cannot be mixed with filename targets, and ResolveSymbolicLinks does not apply to handles. A pool is accepted only as the sole argument and closes through the active parallel runtime.
Execution
Filesystem mutation and handle callbacks are host effects. Filename inputs must contain host text; resident numeric values are rejected without downloading them. Handle properties and listener callbacks are not filename inputs: deletion retains their existing payloads and residency. There is no acceleration kernel or fusion route for deletion itself.
Failures retain their diagnostic identifier. A missing path or unmatched pattern raises RunMat:DELETE:FileNotFound; a directory raises RunMat:DELETE:Directories; filesystem failures raise RunMat:DELETE:PermissionDenied.
Examples
Delete a temporary file
fname = 'scratch.txt';
fid = fopen(fname, 'w');
fclose(fid);
delete(fname);
assert(exist(fname, 'file') == 0);Remove matching log files
logs = ["log-01.txt", "log-02.txt"];
for i = 1:numel(logs)
fid = fopen(logs(i), 'w');
fclose(fid);
end
delete('log-*.txt');
assert(exist('log-01.txt', 'file') == 0);
assert(exist('log-02.txt', 'file') == 0);Delete a string array of paths
files = ["stageA.dat", "stageB.dat"];
for i = 1:numel(files)
fid = fopen(files(i), 'w');
fclose(fid);
end
delete(files);
assert(exist('stageA.dat', 'file') == 0);
assert(exist('stageB.dat', 'file') == 0);Delete filenames in a character matrix
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);
assert(exist('stage1.tmp', 'file') == 0);
assert(exist('stage2.tmp', 'file') == 0);Handle a missing file
caught = false;
try
delete('missing-file.txt');
catch err
caught = strcmp(err.identifier, 'RunMat:DELETE:FileNotFound');
end
assert(caught);Invalidate a graphics handle
fig = figure;
delete(fig);
tf = isvalid(fig);
assert(~tf);Expected output:
tf = 0Using delete with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how delete changes the result.
Run a small delete example, explain the result, then change one input and compare the output.
FAQ
What message IDs does delete produce?⌄
Missing files use RunMat:DELETE:FileNotFound; directories use RunMat:DELETE:Directories; invalid wildcard patterns use RunMat:DELETE:InvalidPattern; filesystem failures use RunMat:DELETE:PermissionDenied; invalid handle inputs use RunMat:DELETE:InvalidHandle.
Can I delete folders?⌄
No. Use rmdir; delete raises RunMat:DELETE:Directories for a directory.
Can I delete several files?⌄
Yes. Pass several path arguments, a string array, a cell of path text, or a character matrix. Targets are processed sequentially.
How do wildcard patterns work?⌄
* and ? match paths relative to the captured filesystem provider's working directory unless the path is absolute. An unmatched pattern raises RunMat:DELETE:FileNotFound.
Do empty arrays raise errors?⌄
Empty string arrays and cells do nothing; an empty string scalar is invalid.
Are resident arguments downloaded?⌄
No. Resident numeric filename inputs are rejected before provider access, including within cells. Handles and listeners may retain resident properties or callback captures; deletion does not download those payloads.
Does delete preserve symbolic links?⌄
By default it removes the link itself. Set ResolveSymbolicLinks to true or exact one to remove its resolved target instead.
Can I inspect failures?⌄
Use try/catch and inspect the exception's identifier and message.
Related Io functions
Repl Fs
addpath · cd · copyfile · dir · exist · fileattrib · fileparts · filesep · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · memmapfile.subsasgn · memmapfile.subsref · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · getvaropts · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · setvaropts · setvartype · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Data
commit · data.copy · data.create · data.delete · data.exists · data.export · data.import · data.inspect · data.list · data.move · data.open · DataArray.chunk_shape · DataArray.codec · DataArray.dtype · DataArray.fill · DataArray.name · DataArray.rank · DataArray.read · DataArray.resize · DataArray.shape · DataArray.write · Dataset.array · Dataset.arrays · Dataset.attrs · Dataset.begin · Dataset.get_attr · Dataset.has_array · Dataset.id · Dataset.path · Dataset.refresh · Dataset.set_attr · Dataset.set_attrs · Dataset.snapshot · Dataset.version · DataTransaction.abort · DataTransaction.commit · DataTransaction.create_array · DataTransaction.delete_array · DataTransaction.fill · DataTransaction.id · DataTransaction.resize · DataTransaction.set_attr · DataTransaction.set_attrs · DataTransaction.status · DataTransaction.write
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.