While Contentful technically does not support changing the id of a content type, Revise can help you migrate existing entries to a duplicate of the content type with the changed id without writing a single line of code.
A summary of what we'll be doing to change the id of a content type in our Contentful space:
- Create a staging environment
- Duplicate the content type whose id you want to change
- Update your content model validations
- Run bulk editing with Revise to duplicate entries and remove the old ones, while updating references to the old entry
- Update your webhooks, previews etc.
- Test the changes and go live
While this process effectively keeps all of your current entry data, it will not preserve older versions of your entries (snapshots), nor comments/tasks assigned to entries whose content type id you are changing.
Let's get started!
Working in a staging environment
What we will be doing is potentially destructive so before we start, we will want to create a staging environment. The idea is to do all of the changes in a separate environment, and only once we confirm that all of the changes worked, we can promote that environment to production. Should anything go badly, we can simply delete the staging environment and start again.
You can create a new environment in Contentful under Settings -> Environments. After you do that, the Contentful web app and you can then switch to the newly created environment from the space/environment switcher top-left. You will know that you are in your newly created environment if it's showing the environment name under your space name in the header.
Don' skip the environment creation step because you risk corrupting your production data.
Duplicating the content type
Once you are in the safety of your staging environment, it's time to duplicate the content type whose id you want to change. In Contentful, click on the Content model menu item and then select the content type. Top right, click on Actions, and then on Duplicate. Give it a temporary name (like Duplicate of X) and set the Api Identifier to the new id you want to give it, then proceed with the duplication
Updating the content model validations
As we will be migrating entries from the original content type to your new duplicate, we will need to tweak our content model validations to allow for referencing the new content type where the old content type was previously allowed.
To do that, in Contentful go to Content model, and for each content type that linked to the old content type make sure to also allow linking to the new one.
If you want to quickly see which content types are linking to the old content type, head on over to contentmodel.io for visual analysis of your content model.
Running the bulk editing with Revise
We are finally ready to run our bulk editing. If you have not already, install the Revise app and then open the Revise dashboard in Contentful (found under Apps - Revise).
The first thing you will want to do is add a new filter for the content type whose id you want to change.
Create a new entry operation
Next, add a Create a new entry operation. This operation generates a new entry for every existing entry matching your filters. Select the new content type and you should see a form for the new entry already pre-filled with shortcodes that will copy the values of the entry based on which the new entry is being generated.
You can learn more about the shortcodes in the Shortcodes documentation.
You will also want to check the "Replace links to reference entry" checkbox found at the bottom of the Create a new entry operation card. This option ensures that any entries that linked to the old entry now link to the new one instead.
Delete entry operation
Finally, you should add a Delete entry operation which will ensure that old entries are deleted as new ones are created.
Run bulk editing
If you have followed the steps above, you can now uncheck the Dry run checkbox at the bottom and click the Run bulk editing button. Depending on the number of existing entries, it could take a few minutes for the migration to the new content type to complete.
Once the process completes, open the Contentful content page to confirm that everything looks good and that your entries have been migrated successfully to the new content type.
Should any errors come up, you can check the logs on the results page for additional insights into what went wrong.
Updating webhooks, preview configuration, etc.
If everything looks good, the final step is to tweak any webhooks and preview configuration if they were referencing the old content type. You will of course also need to update your code to reflect the change in content type id.
Test the changes and go live
Make sure that your project works with the updated content type and that no data was lost. If everything looks good, you can use environment aliases to promote the staging environment to production.