How to change the _type of a document in Sanity.io using the terminal or Postman.
Unfortunately, you've discovered one of the fundamental constraints of Sanity's Content Lake: the _type attribute is immutable and cannot be changed after a document is created. This is by design to maintain data integrity and consistent relationships between documents and their schemas.
When you renamed the document type in your schema, it created a mismatch - the old documents still have the original _type value, but your schema no longer recognizes that type, which is why you can't access them in Studio.
The Solution: Create New Documents
Since you can't modify _type directly (even through export/import), you need to create entirely new documents with the new type. Here's how to approach this:
Option 1: Use the CLI Migration Tool (Recommended)
The Sanity CLI migration tool is built for exactly this scenario:
npx sanity@latest migration create changeDocumentTypeThen write a migration script that:
- Queries documents with the old
_type - Creates new documents with the new
_typeand copies all the content - Optionally deletes the old documents
import {defineMigration, at, setIfMissing} from 'sanity/migrate'
export default defineMigration({
title: 'Change document type from oldType to newType',
migrate: {
async document(doc, context) {
if (doc._type === 'oldType') {
// Create new document with new type
const newDoc = {
...doc,
_type: 'newType',
_id: doc._id.replace('drafts.', ''), // or generate new ID
}
await context.client.create(newDoc)
// Delete old document
await context.client.delete(doc._id)
}
}
}
})Option 2: Manual Script with Sanity Client
You can also write a custom script using the Sanity client:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
token: 'your-token',
apiVersion: '2024-01-01',
useCdn: false
})
const oldDocs = await client.fetch(`*[_type == "oldType"]`)
for (const doc of oldDocs) {
const {_id, _rev, _updatedAt, _createdAt, ...content} = doc
// Create new document with new type
await client.create({
...content,
_type: 'newType',
_id: _id, // Keep same ID or generate new one
})
// Delete old document
await client.delete(_id)
}Important Considerations
- Document IDs: You can keep the same
_idvalues if you delete the old documents first, or generate new IDs if you want to keep both temporarily - References: If other documents reference these documents, those references will still work if you keep the same
_id - Drafts: Remember to handle both published documents and drafts (which have IDs prefixed with
drafts.) - Test first: Always test your migration on a development dataset before running on production
Temporary Workaround
While you're preparing your migration, you can temporarily add the old type back to your schema (just the basic structure) so you can access those documents in Studio. This gives you time to plan and execute the migration properly.
The key takeaway: changing _type requires creating new documents, not modifying existing ones. It's a full data migration rather than a simple update, but the CLI migration tool makes this process much more manageable.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.