Releasing Safe Mode from script

There is a way to terminate a Safe Mode session from a script?

I found this way to unroll the changes:

:local history [/system/history/print detail as-value where floating-undo=yes]
:local historylength [:len $history]

:while ($historylength > 0) do={
    /undo
    :set $historylength ($historylength - 1)
}

By the way this doesn’t terminate Safe Mode.

There is a direct way? A more robust way maybe?

There is way to commit changes instead?

Context: http://forum.mikrotik.com/t/a-mikrotik-safe-mode/171110/1