How to change a content type’s machine name in Drupal 8

With Drupal Console and Drupal 8’s Configuration Management

I recently had to rename a content type’s machine name after I used the wrong format which would have caused inconsistency in how the various content types on my site were named. Luckily, it’s easy enough to change a machine name using Drupal Console and the Configuration Management tools built into Drupal 8.

Run this to export your site’s configuration to your configuration sync directory (it will end up somewhere with a path like sites/default/files/config_Iz4K9eLC5r…JEPq1K5KAc/sync):

drupal config:export

In something like PhpStorm or the command line, do a search and replace across the whole sync directory, swapping instances of your old_content_type machine name for your new_content_type machine name. This will update the contents of the config files, but their filenames will still need changing.

To change the filenames, use the terminal to cd to the config sync directory, and run the following command:

rename 's/old_content_type/new_content_type/g' ./*

That will update the filenames of the config files. Finally, update your site’s database by runing this Drupal Console command:

drupal config:import

You should now see the new content type machine name is in use. If there are any issues, try rebuilding your cache:

drupal cache:rebuild