This error is indicative of a Corrupted Database Project File. See: Result and Error Codes
The “Audacity failed to write a file” error is a red herring and is triggered by the corruption.
In many cases the corruption can be repaired. See the first post here: Corrupt or Otherwise Broken Audacity Project Recovery
I am glad that you found a helpful trick to improve performance. “Undo” information and smartclips can sometimes bog down a project. I sometimes do Ctrl-A, Ctrl-C, Ctrl-N, Ctrl-V, skipping the “smart clip” paste.