Factory resetting your Mac, iMac, MacBook, or MacBook Pro is easier with newer versions of macOS but you still need to learn it to learn how you can fix software problems by reinstalling macOS
As long as you don't erase the hard drive when you're in Disk Utility, you can keep all your data on your Mac, and fix the problem you're having with installing software.
How to Factory Reset Macs?
First: macOS Recovery
You will need to enter macOS recovery several times to complete the steps to reset your Mac.
For Apple Silicon Macs
Shut down your Mac, then head to the Apple menu and choose Shut Down, or hold down the Power button and press Shut Down when the pop-up dialog appears. Once it shuts down, hold down the power button as if you were restarting your Mac, but hold the button down, and after a few seconds, you'll see a window called Startup Options. Click the gear options icon, then Continue.
For Intel Macs
Make sure your Mac is connected to the Internet, restart and while holding Cmd + R until the Apple logo appears on the screen, release the keys.
Now.. clear data
Once you activate this step all the data will be erased from your Mac, and there will be no way to restore the data, so make sure to backup all your important data first.
Once in your macOS recovery copy, you should open Disk Utility from the options on the screen, then click Macintosh HD Drive under the Internal heading on the left side of the window.
Now click on Erase in the toolbar at the top of the window, a dialog box will appear on the screen, and inside it, you have to name the new Macintosh HD drive. Set the format to APFS or Mac OS Extended, then click Erase (the Erase Volume Group might appear instead).
Make sure you complete the same process for any other drives under Internal, any external hard drives or USB devices should appear under External, so you won't erase them, but it's best to keep them offline anyway. Exit Disk Utility, and you will be returned to the main window.
Now.. Reinstall macOS on the device
Once you're back in the main macOS Recovery window, you can now follow this step to reinstall a fresh copy of macOS on the device.
As long as you followed the previous steps, you will not be able to use the device again, before reinstalling the operating system that was erased from the hard drive.
In this step, make sure your Mac is plugged into the charger and connected to Wi-Fi, but keep in mind that it will ask for the network password. Click Reinstall macOS in the main window.
Your Mac will take you through a set of on-screen instructions to complete the reinstallation of macOS, the instructions are self-explanatory. and by following them; Your Mac will download the latest version of its software from the web and install it on the machine.
But keep in mind that this step may take some time.
Finally..don't forget to set up macOS
When you set up macOS, you'll be greeted with the setup assistant, the same process you did when you first bought the device, and the instructions are easy and won't take much.