File#unlink will raise an exception on Windows unless the file is closed. You should change to first close then unlink the file.