REGR: to_stata tried to remove file before closing it
Spin-off from #39047:

If an error occurred during `to_stata`, the code tried to first remove the file and then close the file handle:

1. removing the file failed on Windows (it is still opened)
2. It created an empty file when closing the handle
