Delete all output in notebooks
Print
and Message
and so on are generated cells, like the output from evaluating input. This, from the docs for Cells
, is what you need (just substitute your actual notebook object for EvaluationNotebook[]
in the code):
NotebookDelete[Cells[EvaluationNotebook[], GeneratedCell -> True]]
I'd use Map
instead of Do
but in any case just replace CleanNotebook[nb]
with NotebookDelete[Cells[nb, GeneratedCell -> True]]
in your code.
Three additional alternatives:
- Call
CleanNotebook
with two arguments in yourDo
loop:
CleanNotebook[nb, {"Output", "Print"}];
- Replace the line
CleanNotebook[nb];
in yourDo
loop with
FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]];
- Replace your
CleanNotebook
with
cleanNotebook[nb_: SelectedNotebook[]] := SetOptions[nb, NotebookEventActions ->
{"WindowClose" :> FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]]}]