Before erasing the hard drive , make sure in Mac system preferences > all inbuilt applications box should be turned as on including iCloud Drive , desktops and documents Use iCloud Drive to store documents on your Mac and iOS devices - Apple Support , safari , keychain box and others like notes , calendars , contacts , photos , mails ( an IMAP account should be configured ) , reminders ....etc see the article
Set up iCloud features on Mac - Apple Support
Once you erase the hard drive and reinstall Mac OS Erase and reinstall macOS - Apple Support and set up new Mac Set up your Mac - Apple Support the same Apple ID and password with all accounts credentials known to you has to be signed in Sign in with your Apple ID - Apple Support Mac system preferences the iCloud has to be set up Set up iCloud on your Mac - Apple Support again iCloud Drive is to be turned on Set up iCloud Drive - Apple Support
And inbuilt apps boxes are to be turned on the data stored from apps and that was configured with iCloud will again sync in the new operating system ( make sure your iCloud data should not never exceed the designated storage limit ) .