# clear value from a bunch of symbols

I have a bunch of symbols which are collected as a list inside a variable within a bit of code e.g.

a=1;b=2;
par={a,b};


I want to clear the values of both a and b using only a reference to par. Here is my attempt and the error produced:

Clear[Sequence@@par]
... Clear::ssym: Sequence@@par is not a symbol or a string.


I can understand why this gives an error but can't figure out a way out of it.

• par is simply {1, 2} here, how would it know about the symbols a and b? Mar 31, 2017 at 9:47
• To amplify Marius's comment, look at the result of ??par. Mar 31, 2017 at 9:48
• I do get the idea. Is there a way, though ? Mar 31, 2017 at 9:50
• An approach similar to this is one possibility: par = Hold[a, b]; and much later, Scan[Clear, par] or Clear @@ par. Mar 31, 2017 at 10:00

This will not work. Because you used Set, not SetDelayed. After executing this code:

a = 1;
b = 2;

par = {a, b}
OwnValues[par]

(*
{1, 2}
{HoldPattern[par] :> {1, 2}}
*)


par isn't a list with a and b. par is a list that contains {1, 2}.

a = 1;
b = 2;

par := {a, b}
par
OwnValues[par]

(*
{1, 2}
{HoldPattern[par] :> {a, b}}
*)


Now you must take list of values from OwnValues rule:

Hold[par] /. OwnValues[par]

(* Hold[{a, b}] *)


And if you want clear a and b:

Hold[par] /. OwnValues[par] /. Hold[{args__}] :> Clear[args]

par
{a, b}

(*
{a, b}
{a, b}
*)


Definition for par preserved and for a and b cleared

As others correctly pointed out, there is no way to establish variable names after the fact in this case. But if you have traced your code, which is a big if, of course, but imagine you did:

trace = Trace[
a = 1; b = 2;
par = {a, b}
];


Then you can fish out the variable names that were used during assignment and clear them:

Cases[
trace,
HoldPattern[par = vars : {___Symbol}] :> ClearAll @@ Unevaluated @ vars,
All
];