How to reinstall your Drupal 8 website from the active configuration

We break things quite often. Especially when it comes to Drupal 8 development, which, for most developers, is full of new revelations, unexpected failures and mismatched expectations.

So at some point, you may face a situation when one of your environments (it can be local, staging, or even production one) has to be fully rebuilt from scratch. Not a big deal, you might think, as all the configuration is sitting in the «config» directory under version control. So you do a drush si minimal -y, bravely dropping the database, followed by drush cim and then this is what happens:

[error] The import failed due for the following reasons: 
Site UUID in source storage does not match the target storage.

Obviously, the UUID of the website has been modified during reinstall, and your configuration doesn’t belong to the freshly installed instance anymore. Luckily enough it’s easy to fix. Use your local or any stable working environment and grab the UUID first:

drush cget uuid

Next, set the UUID of the problematic instance to the one we’ve just copied, i.e.

drush cset uuid c7635c29-335d-4655-b2b6-38cb111042d9

Lastly, re-run the config import and we’re back online.

PS. Of course, copying the stable version of the database over the problematic one can be a solution, but it will not work if you’re looking for a repetitive way to reinstall the website (i.e. «rebuild on each commit»). Besides, if you have to do direct database operations to recover, in most cases you do (or get it) wrong.