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]]

enter image description here

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:

  1. Call CleanNotebook with two arguments in your Do loop:

CleanNotebook[nb, {"Output", "Print"}];
  1. Replace the line CleanNotebook[nb]; in your Do loop with

FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]];
  1. Replace your CleanNotebook with

cleanNotebook[nb_: SelectedNotebook[]] := SetOptions[nb, NotebookEventActions -> 
 {"WindowClose" :>  FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]]}]