This process should only be followed if you are prepared to factory reset the Android device. This means that any previously installed app along with its data will be deleted. These instructions are best followed using Santoku Linux, although this is not a requirement.
Android Device with USB cable
A Linux Host/VM (not specifically required, but these instructions are best followed using Linux (Santoku))
Internet Connection (to download the factory image)
Download the Google factory image here: https://developers.google.com/android/nexus/images. For example, navigate to the “hammerhead” for Nexus 5 (GSM/LTE) for Nexus 5 images.
Connect the device to the Linux environment using the USB cable. If you are using a VM, ensure that the device has been properly passed through.
Start the device in fastboot mode with one of the following methods:
Using adb: With the device powered on, execute: “adb reboot bootloader”
Using a key combo: Turn the device off, then turn it on and immediately hold down the relevant key combination for your device. For example, to put a Nexus 5 (“hammerhead”) into fastboot mode, press and hold Volume Up + Volume Down + Power as the device begins booting up.
If necessary, unlock the device’s bootloader by running: “sudo fastboot oem unlock”. The device will show you a confirmation screen. (This erases all data on the target device.)
Open a terminal and navigate to the unzipped system image directory.
Execute the flash-all script. This script installs the necessary bootloader, baseband firmware(s), and operating system. You can do this by running: “sudo ./flash-all.sh” from within the directory.
When this process completes, the device will reboot, and you will have successfully flashed to a stock state.