clearvars — Clear workspace variables with optional exclusions in MATLAB and RunMat.
clearvars removes variables from the active workspace, with optional -except exclusions. Named and exclusion forms follow MATLAB semantics for selectively clearing bindings.
Syntax
clearvars()
clearvars(name_or_option, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name_or_option | StringScalar | Variadic | — | Variable names and options such as '-except'. |
Returns
| Name | Type | Description |
|---|---|---|
ans | NumericArray | Empty 0x0 return value. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | The '-except' option appears more than once. | clearvars: duplicate -except option |
| — | An unknown option beginning with '-' is provided. | clearvars: unsupported option |
| — | The '-except' option is provided without exclusion names. | clearvars: -except requires at least one variable name |
| — | Workspace snapshot is unavailable while processing '-except'. | clearvars: workspace state unavailable |
| — | Character-array argument is not a row vector. | clearvars: character array inputs must be a row vector or scalar text value |
| — | An argument is not a string scalar/array or row character vector. | clearvars: expected variable names as character vectors or string scalars |
How clearvars works
clearvarswith no inputs clears all workspace variables created in the current session.clearvars xremoves the variablexwhile leaving other workspace bindings intact.- Multiple names can be supplied as separate inputs or command-form tokens, for example
clearvars x y z. clearvars -except x yremoves all active workspace variables exceptxandy.clearvars a b c -except bremoves only selected variablesaandc, preserves selected exclusionb, and leaves variables outside the selected clear set untouched.- Function-call forms accept variable names as string scalars, row character vectors, or string arrays.
- Clearing a name that does not exist is a no-op.
clearvarsis a sink builtin and does not produce a meaningful output value.
Does RunMat run clearvars on the GPU?
clearvars performs host-side workspace mutation only. It does not launch GPU kernels, does not participate in fusion, and does not depend on acceleration providers.
Examples
Clear the entire interactive workspace
x = 1;
y = magic(3);
clearvarsExpected output:
% x and y are removed from the workspaceClear selected variables
a = 1;
b = 2;
c = 3;
clearvars a bExpected output:
% a and b are removed; c remains definedKeep selected variables while clearing the rest
raw = 1;
answer = 42;
tmp = 3;
clearvars -except answerExpected output:
% answer remains defined; raw and tmp are removedClear selected variables while preserving exclusions
a = 1;
b = 2;
c = 3;
untouched = 4;
clearvars a b c -except bExpected output:
% a and c are removed; b and untouched remain definedUsing clearvars with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how clearvars changes the result.
Run a small clearvars example, explain the result, then change one input and compare the output.
FAQ
How is clearvars different from clear?⌄
clearvars focuses on workspace variables and supports the -except option for preserving named bindings while clearing the rest.
Can I clear only one variable?⌄
Yes. Use clearvars x or clearvars("x") to remove one variable, and pass multiple names to clear more than one binding in a single call.
Does clearvars -except x clear figures or the console?⌄
No. It only mutates workspace variables. Use close all for figures and clc for the visible console.
Does GPU residency matter?⌄
No. clearvars operates on session bookkeeping, not on array kernels or provider-managed computations.
Related Introspection functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how clearvars is executed, line by line, in Rust.
- View the source for clearvars 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.