clearvars — Clear variables from the active workspace, with optional exclusions.
clearvars removes variables from the active RunMat workspace. RunMat supports MATLAB-style command forms such as clearvars x y, whole-workspace exclusion forms such as clearvars -except keep, and selected-variable exclusion forms such as clearvars a b -except b, updating the session snapshot so hosts can drop cleared bindings.
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.
How RunMat runs 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 definedFAQ
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 works, line by line, in Rust.
- View clearvars.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.