Key Command on Macro issue

I have set up a Macro for Remove Unused Media + Empty Trash and assigned a Key Command to it.
If I do these two actions manually it works fine, but when I trigger the macro via key command it -sometimes- gives me the “serious error has occurred” message. Then I save and close. On re-open I get the safe mode prompt, then when opening I get a list of supposedly missing files, which are the files no longer used and deleted. So obviously Cubase crashes before it can keep track of the audio files being deleted.