In this article we're going to show you how to perform a clean install of macOS. This is often a handy option if your Mac is slow or otherwise misbehaving.
We'll start by showing how to do this for the latest version of macOS (macOS Sierra, at time of writing), before moving on to the far tricker business of installing a clean copy of El Capitan, Yosemite or some earlier version of OS X. If that's what you're looking on, it may be worth taking a look at another article where we focus on that entirely: How to download and install old versions of OS X on a Mac.
What you need
You'll need an 8GB or larger removable USB flash drive to hold the installer file for our chosen version of macOS. (We're going to erase it, so make sure it doesn't contain any valuable data.) You will also need admin privileges.
Before you start, remember to back up. Full instructions here: How to back up a Mac.
Ready? Let's get started.
Format the drive
We're going to re-download the installer file for our chosen version of macOS and store it on a removable drive. The drive you use needs to be formatted as a Mac OS Extended (Journaled) volume with a GUID Partition Table.
To format the drive, go to Applications > Utilities and open Disk Utility. Select the drive and click Erase. (The name of the disk needs to be "Untitled", if the Terminal commands below are to work, so rename it if necessary.)
Select Mac OS Extended (Journaled) as the format type. Click Erase.
Get the Sierra install file
If you're reinstalling the latest version of macOS, our next step is easy: just go to the Mac App Store and search for its name. At time of writing, that means macOS Sierra. (For Sierra, we can save you a trip: just click here.) Hit Download.
You may get a warning message at this point, telling you that this version of macOS 'is already installed on this computer. Would you like to continue?' Click Continue, then enter your Apple ID and password. The installer file will be multiple gigs (El Capitan was about 6GB, for example), so downloading can take a while.
When it's finished downloading the installer will launch automatically, but we don't want to use it yet, so press Apple + Q (or rather Command + Q) to quit out of the installer. Find the installer file (which should have appeared in the Applications folder) and save it to an external flash drive.
You can skip the next step, where we'll be showing how to dig up the installer file for an older version of Mac OS X. Our next step will be to create a bootable USB disk.
Get the install file for an older version of Mac OS X
For older versions of Mac OS X it's a bit harder to get the install file: Apple doesn't offer downloads of outdated OS versions through the normal store front, and searching for the name won't find what you're looking for.
Instead, we'll use the Purchased section of the App Store. (This only works if you have installed the version before - it won't let you revert to an older version that you skipped.)
Make sure you're logged into the Mac App Store with the Apple ID you used to update to the version of OS X you're looking for, then click Purchased in the menu bar along the top. Scan down the list of apps you've purchased or download for free (which is in order of when you downloaded them) and find the name of your chosen version: El Capitan, Yosemite or whatever. Click download.
(For more advice on getting hold of older editions of Mac OS, see How to download and install old versions of OS X on a Mac.)
Create a bootable USB disk
Next we need to create a bootable USB disk so that we can install a new copy of macOS on the Mac from the flash drive. We've got full instructions on how to do that here - How to make a bootable Mac OS X install drive - but we'll run through the basics again. We're going to create the bootable drive using Terminal.
Connect the removable drive to your Mac, and make sure it's called Untitled - rename it if necessary. Make sure the installer (or at least a copy of it), called Install macOS Sierra.app or similar, is in its default location in your main Applications folder (/Applications).
Create a bootable USB disk: Terminal
Select the text of the following Terminal command and copy it (Command + C):
sudo /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ El\ Capitan.app --nointeraction
Go to Applications > Utilities and double-click Terminal to launch it. Paste (Command + V) the copied command into Terminal and hit Return. Type your admin-level account password when prompted, and press Return again.
If you see the message 'To continue we need to erase the disk at /Volumes/Untitled. If you wish to continue type (Y) then press return', type Y and press Return; if not, don't worry.
When the procedure is finished (it can take anywhere from a couple of minutes to half an hour), you'll see Copy Complete and Done.
Install from installer drive
Now we'll install a fresh copy of macOS from the installer drive.
With the boot drive connected, start up - or restart - your Mac while holding down the Option key (also known as Alt). This will take you to Startup Manager. Choose to install your chosen version of macOS from the drive. Select 'Disk Utility' and your hard drive, then Erase. Go back to the main menu and choose Install macOS.
Once installation is complete, you can restoring apps and settings from a Time Machine backup, or download them again manually.