Quit the Kernel and start new session automatically

The question is how to quit the kernel, start a new session automatically, and to be able to run such procedure from e.g. Button.

This is especially useful when you can't count on Dynamic (80560) or you don't want to care if a given cell is currently visible.

Motivation

when developing packages I'm often deleting all output, quitting kernel, loading the package. This can be done in two steps/shortcuts. I want one :)

There is closely related/duplicate, but "too old to migrate" question, without final anwer, in SO:

Quit Kernel in Mathematica...

quitAndEvaluate[
Print @ $SessionID ]  25183094379509806957 25183094575602627552 • quitAndEvaluate[] will restart kernel without aby additional tasks. It may be useful if you want to check init.m or Autoload behaviour. • You can provide procedures delayed in symbol definition (not so obvious since definitions are cleared in a process: ext := (Print[1]; Print[2]); quitAndEvaluate[ext]  Code SetAttributes[quitAndEvaluate, HoldAll]; quitAndEvaluate[proc_Symbol] := Unevaluated[quitAndEvaluate[proc]] /. OwnValues[proc]; quitAndEvaluate[after___] := With[{ currentSetting = CurrentValue[$FrontEnd, "ClearEvaluationQueueOnKernelQuit"]
},
Composition[
(SelectionMove[#, All, Notebook]; SelectionEvaluate[#]) &
,
CreateDocument[#, Visible -> False, WindowSize -> {Automatic, 100}] &
,
Cell[#, "Input"] & /@ # &
,
BoxData /@ # &

]@{

MakeBoxes[ CurrentValue[ $FrontEndSession, "ClearEvaluationQueueOnKernelQuit" ] = False; ] , MakeBoxes[ Quit[]; ] , MakeBoxes[ after; CurrentValue[$FrontEndSession,
"ClearEvaluationQueueOnKernelQuit" ] = currentSetting;
NotebookClose[EvaluationNotebook[]]; $Line = 0;] } ]  There is a funny thing with WindowSize. Even though the notebook is invisible, you have to set explicitly any dummy value, otherwise it won't work. :) Description It basicaly creates a new invisible notebook with 3 cells and evaluates it. The first cell sets our option to False. The second Quit[]s the Kernel. The third restores FrontEnd option. Evaluates your code and deletes that notebook. • I think there should be a $Line=0; after NotebookClose[EvaluationNotebook[]]; May 18 '15 at 22:38
• @Kuba +1 for the neat solution. I find though, that the responsiveness of "ClearEvaluationQueueOnKernelQuit" is not really what one might have hoped for given that eking out a few seconds savings is the main point. One would expect that the time to complete the action of a corresponding button press would be ~QuitTime + LaunchTime but my usage indicates that the actual behaviour is more like ~QuitTime + LaunchTime+5s which kind of makes it not worthwhile. Aug 16 '20 at 11:25
• Also, don't you want it to be $FrontEndSession all the way through? (n.b. the first setting of currentSetting uses $FrontEnd). Having said this, using $FrontEnd all the way through somehow shaves a couple of seconds but at around 10s to start with it is still kind of unwieldy. But a warning for accidentally setting$FrontEnd persistently, Aug 16 '20 at 11:36
• @RonaldMonson yes, \$FrontEndSession is more appropriate to get the currentSetting. About timings, I will check tomorrow. About responsiveness, I surely agree but that's all I was able to come up with.
– Kuba
Aug 16 '20 at 22:09