Yes, a factory reset will work.
First, you will need to boot into recovery mode, this is device-specific, so you will need to google how it's done on your phone. But on mine for example: hold power and volume up until a menu opens up, select recovery with volume up, press volume down, and when the "no command" screen opens up, press power and volume up.
Once you're inside recovery mode, select "factory reset" or "perform factory reset" with the volume buttons, and press the power button. Then select yes, and press the power button.
After the factory reset is completed, it will ask for your Google account credentials. If it asks for the previous password, tap "use Google account instead". If your device doesn't have a Google account or you don't know the password for it, look up your phone on hardreset.info and select "bypass frp" or "bypass factory reset protection".