# How to clear all variables except one?

I have a lot of variables: $a,b,c,d, \ldots$ I want to clear the values of all of them except $b$. It's really lengthy to write

Clear[a];
Clear[c];
...


Is there any command which clears all variables except a chosen one?

-

## migrated from math.stackexchange.comSep 23 '12 at 3:44

This question came from our site for people studying math at any level and professionals in related fields.

An alternative that doesn't require protecting or using private contexts:

Clear @@ DeleteCases[Names@"*", "b"];

-
Nice one, frog +1. What about the Timings? No impact? –  belisarius Sep 23 '12 at 8:38
@belisarius Timings? I don't really see a reason for there to be a significant difference in timings... –  The Toad Sep 23 '12 at 16:25
@rm -rf, I was wondering how to use this technique if there are more than one variables? –  preeti Jan 21 '14 at 2:38
@preeti You mean you want to delete all variables except a few specific ones? You can replace "b" above with your variables separated by |, like "b" | "c" | "d". –  The Toad Jan 21 '14 at 2:59
Excellent. If you could include that in your answer, it will make it easy for searching.Thanks. –  preeti Jan 21 '14 at 4:08

You may want to place your special variable(s) in a separate context and then only clear Global*. This is not the same as belisarius' recommendation. Instead create the variable b in a different context to begin with and append this context to the context path:

AppendTo[$ContextPath, "special"]; specialb = 5; (* fully qualified name on first use *) a = 1; c = 2; a + b + c  8  Clear["Global*"] a + b + c  5 + a + c  - The solution is to first protect b, then clear all variables in current context, and then unprotect b again: Protect[b];Clear[Evaluate[$Context<>"*"]];Unprotect[b]


By protecting b, you prevent it from getting cleared (Mathematica will complain, but do the right thing anyway). Note that the \$Context construction makes sure that you clear the correct symbols if you have e.g. chosen to have an unique context for your notebook.

Also note that if you have defined variables in other contexts, too (e.g. using packages) which you also want to have cleared, you'll have to explicitly clear those contexts as well.

-

What I would to is quite simple: simply Block the variable you don't want to clear:

a=1;b=2;c=3;
Block[{a},
Clear[a,b,c]
]

{a, b, c}

(* {1, b, c}  *)


This solution allows you to deal with symbols rather than their string names. You can get fancier and create a closure which would encapsulate your original list of symbols and take those symbols which should not be cleared:

ClearAll[makeClearF];
SetAttributes[makeClearF, HoldAll];
makeClearF[syms__Symbol] :=
Function[Null, Block[{##}, Clear[syms]], HoldAll];


So that, for example

clear = makeClearF[a, b, c];

a=1;b=2;c=3;
clear[a]; (* clear all but a *)
{a,b,c}

(* {1,b,c}  *)


or

a=1;b=2;c=3;
clear[b,c]; (* clear all but b and c *)
{a,b,c}

(*  {a,2,3}  *)


I want to stress that this solution is different from those based on string names in that parsing is not involved. Which one is a better fit depends on the circumstances.

-

Also, preserve it in a "private" context prior to the cleaning:

b = 1;
myCtxb = b;
Clear["Global*"];
b = myCtxb
(* 1 *)

-

One other alternative is to Compress[b] and copy the resulting string to your clipboard, then do the global clear or kernel quit, and then b=Uncompress["..."]. This is not the most elegant solution, obviously, but it's a useful technique to be aware of.

-