4

I have multiple notebooks open, with similar variable declaration (am studying test cases), and I want all notebooks to be independent of each other, and also forget it's own evaluation after finishing. I have added

ClearAll["Global*"]

at the top of the notebook. Also done: Default Kernel as Local. Plus assigned Notebook's Default Context as Unique to this Notebook

But it still retains values.

I just tried adding Quit at the end of the notebook, but each time I Evaluate Notebook again (after it has done Quit once), my program crashes. I put Quit at the beginning of the notebook and did Evaluate Notebook but it compiled the first cell with Quit and then it stops there.

I am out of ideas, any suggestions?

I need this to be default - never having dependence of notebooks on each other and never remember values after ending one evaluation... I want it automated (right now, I have to do Evaluation > Quit Kernel after each evaluation...

ZeroTwo
  • 143
  • 3
  • 1
    Since you've asked different contexts to be assigned to each notebook, their variables are no longer in the Global` context, so ClearAll["Global"]is doing nothing. TryClearAll[StringJoin[Context[], ""]]` instead. – MarcoB Jun 01 '23 at 12:00
  • I got error "ClearAll:: Context[]<>* is not a symbol or a string." with ClearAll[StringJoin[Context[], "*"]] but I tried with ClearAll[Evaluate[Context[] <> "*"]] and it works perfectly; thank you very much! – ZeroTwo Jun 01 '23 at 12:56
  • 1
    Great! I've summarized my comment and your modification in an answer below. – MarcoB Jun 01 '23 at 13:19
  • Possible duplicate: https://mathematica.stackexchange.com/questions/82803/quit-the-kernel-and-start-new-session-automatically – Michael E2 Jun 01 '23 at 15:23

2 Answers2

4

Rather than try to clear all defined variables I like to just quit the kernel to get a clean slate.

To accomplish your goal I would place a single cell at the top of each notebook with the expression

Quit[]

and then just Cmd-A to select all cells and then evaluate them. First the kernel will quit and then all the other cells will evaluate. If you find that when it hits the Quit[] it no longer evaluates the other cells, then evaluate the following

CurrentValue[$FrontEnd, "ClearEvaluationQueueOnKernelQuit"] = False

I have no idea why that isn't the default value for that option.

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • 3
    Thanks for sharing this! One idea why it's the default: You want the kernel to stop, as in stop now, give me back control cuz I made a mistake. Another reason: the rest of the queue often depends on initialization, without which its execution fills the notebook with error messages. -- Alternatively they could have had two kinds of quit, like the C and AC buttons on old calculators. – Michael E2 Jun 01 '23 at 15:22
  • That makes sense - I always considered the menu item Evaluate->Quit Kernel to be the "stop, stop now" command because it does it even when the kernel is hanging. I probably found ClearEvaluationQueueOnKernelQuit as an option shortly after I started using Mathematica so it seems natural to me. – Jason B. Jun 01 '23 at 19:00
  • Yeah, it's unlikely a user selects a sequence of cells in the middle of which is Quit[] and the following cells require initialization. That's their fault. And you can't abort by typing Quit[] in the middle of an evaluating queue. -- BTW, do you know what happens if ClearEvaluationQueueOnKernelQuit is false and you kill the kernel externally? Does evaluation restart? I guess you can always kill the FE if you do something stupid, but you lose unsaved work. – Michael E2 Jun 01 '23 at 19:18
  • 2
    The Evaluation->Quit Kernel is a nuclear option, will not restart any computations after that – Jason B. Jun 01 '23 at 20:15
2

Since you've asked for different contexts to be assigned to each notebook, their variables are no longer in the Global` context, so ClearAll["Global*"] is doing nothing for you. You can extract the name of the current context with Context[], then use ClearAll[Evaluate@ StringJoin[Context[], "*"]] instead.

MarcoB
  • 67,153
  • 18
  • 91
  • 189