clear — Clear variables from the active workspace in MATLAB and RunMat.
clear removes variables from the active workspace. It supports clear, clear all, and named-variable forms (for example clear x), following MATLAB and RunMat workspace-clearing semantics.
Syntax
clear
clear(name, ...)
ans = clear()
ans = clear(name, ...)Inputs
name- Type
Any- Required
- Variadic
- Description
- Variable names to clear. 'all' clears entire workspace.
Returns
ans- Type
NumericArray- Description
- Empty 0x0 return value.
Errors
- When
- Character-array argument is not a row vector.
- Message
clear: character array inputs must be a row vector or scalar text value
- When
- A target argument is not a string scalar/array or row character vector.
- Message
clear: expected variable names as character vectors or string scalars
- When
- The current workspace cannot be read or mutated.
- Message
clear: workspace operation failed
- When
- An onCleanup callback or dynamic function invalidation fails.
- Message
clear: cleanup or function-cache invalidation failed
Behavior and placement
clear with no inputs clears all workspace variables created in the current session.
Bare clear removes workspace variables. clear('all') and command-form clear all additionally request dynamic-function cache invalidation when no workspace variable named all shadows the directive.
clear x removes the variable x while leaving other workspace bindings intact.
Multiple names can be supplied as separate inputs or command-form tokens, for example clear x y z.
Active onCleanup objects are invoked before their workspace bindings are removed.
After a successful clear, removed variables are no longer available to later statements and the workspace snapshot is refreshed so hosts can drop cleared bindings.
Accepts variable names as string scalars, row character vectors, or string arrays. A matching workspace binding takes precedence over an option-like name; otherwise all, variables, functions, and mex select their respective clear directives.
Clearing a name that does not exist is a no-op.
clear is a sink builtin and does not produce a meaningful output value.
GPU support: Runs on the host CPU because it mutates the session workspace rather than array contents.
clear performs host-side workspace mutation only. It does not launch GPU kernels, does not participate in fusion, and does not depend on acceleration providers.
Legacy examples and displayed results
Clear the entire interactive workspace
x = 1;
y = magic(3);
clear
Expected display: % x and y are removed from the workspace
Use MATLAB-style command form
a = 42;
clear all
Expected display: % a is removed from the workspace
Accessing a cleared variable raises an error
x = 7;
clear;
x
Expected display: Undefined variable: x
Clear one variable while leaving others intact
x = 1;
y = 2;
clear x
Expected display: % x is removed from the workspace while y remains defined
Clear multiple named variables in command form
a = 1;
b = 2;
c = 3;
clear a b
Expected display: % a and b are removed; c remains defined
Example
clear one name
x = 1; y = 2; clear x; z = exist('x','var')Using clear with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how clear changes the result.
Run a small clear example, explain the result, then change one input and compare the output.
FAQ
Does clear remove every variable in the current session?⌄
Bare clear clears workspace variables. clear all also requests dynamic-function cache invalidation unless a workspace variable named all shadows the directive. Named forms such as clear x remove only the specified binding.
Can I clear only one variable?⌄
Yes. Use clear x or clear("x") to remove one variable, and pass multiple names to clear more than one binding in a single call.
Does clear return anything?⌄
No meaningful value is returned. clear is a side-effecting builtin whose purpose is to mutate workspace state.
Does clear affect plotting state or the console?⌄
It does not close figures or clear the visible console. Bare clear removes workspace variables; clear all can also invalidate dynamic functions. Use close all for figures and clc for the console.
Does GPU residency matter?⌄
No. clear operates on session bookkeeping, not on array kernels or provider-managed computations.
Related Introspection functions
addlistener · addParameter · addprop · assignin · builtin · cancel · class · clearAllMemoizedCaches · clearCache · clearvars · dbclear · dbstack · dbstatus · dbtype · eval · evalc · evalin · feval · findprop · func2str · functions · get.p · getcallinfo · getmethod · inputname · inputParser · isa · ischar · isdeployed · iskeyword · ismethod · isobject · isstring · isUnderlyingType · isvalid · keyboard · loadobj · localfunctions · matlab.metadata.DynamicProperty.delete · memoize · metaclass · mfilename · mislocked · mlock · munlock · namelengthmax · narginchk · nargoutchk · new_object · notify · numArgumentsFromSubscript · onCleanup · parse · saveobj · set.p · stats · str2func · subsasgn · subsref · superclasses · underlyingType · verLessThan · version · which · who · whos
Source you can read
Every RunMat function's source is public. Read exactly how clear is executed, line by line, in Rust.
- View the source for clear in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.