1
$\begingroup$

Is is possible to set up a Mathematica notebook so that all outputs are automatically deleted when the notebook is closed? I was trying to look for some function like OnNotebookClose[]. Any ideas?

The main reason for wanting something like this is because some of the output I have is dynamic and often gets really messy when I re-open the notebook and the output is partially undefined or unformatted.

Perhaps trickier: in the case of an unsaved notebook, when asked whether I want to save the notebook, would it still be possible to click "Cancel" without erasing the output cells? This is just to avoid accidentally clicking on the window close button

enter image description here

$\endgroup$
2
  • $\begingroup$ See NotebookEventActions. Also, you might want to consider doing something in the Initialization of the DynamicModule and similar outputs $\endgroup$
    – Lukas Lang
    Jul 26, 2021 at 14:31
  • $\begingroup$ You can look at 'FrontEndTokenExecute["DeleteGeneratedCells"]', it deletes all output cells. $\endgroup$ Jul 26, 2021 at 14:43

1 Answer 1

2
$\begingroup$

You can use SetOptions and FrontEndTokenExecute["DeleteGeneratedCells"] in a cell with the following implementation:

nb = InputNotebook[]
SetOptions[nb, 
  NotebookEventActions -> {"WindowClose" :> 
     FrontEndTokenExecute["DeleteGeneratedCells"]}];

Running this in your notebook, will delete all output cells when you close the notebook.

$\endgroup$
5
  • $\begingroup$ Exactly what I was trying. Thanks! Perhaps trickier: in the case of an unsaved notebook, when asked whether I want to save cells, would it still be possible to click "Cancel" without erasing the output cells? This is just to avoid accidentally clicking on the window close button. Just wondering, see the edit. $\endgroup$
    – sam wolfe
    Jul 26, 2021 at 15:21
  • 1
    $\begingroup$ @samwolfe you should be able to simply check whether the notebook had been saved using e.g. NotebookFilename (which returms $Failed for unsaved notebooks) before deleting the cells $\endgroup$
    – Lukas Lang
    Jul 26, 2021 at 15:25
  • $\begingroup$ I was playing around with it, it seems even if you cancel the close; the outputs are still removed. I may look into it a bit more, maybe check out some global options. You could set the global option for autosave On, this will save the notebook after any cell is ran(but can save a corrupted notebook so do at your own risk). With autosave on, you could not save when you close and reopen the notebook to preserve output. However, it probably is more trouble to implement then its worth. $\endgroup$ Jul 26, 2021 at 15:28
  • $\begingroup$ Funny enough, if the notebook is already saved, this does not work. $\endgroup$
    – sam wolfe
    Jul 26, 2021 at 16:02
  • $\begingroup$ @samwolfe I guess you might need to save the notebook again after modifying it? $\endgroup$
    – Lukas Lang
    Jul 26, 2021 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.