How to change _type of existing documents in Sanity?

48 replies
Last updated: Jan 28, 2026
Is there a way to change
_type
of a document? I changed
name
in the schema but it didn't change in the old documents and now I can't access them. I tried exporting, changing the
_type
manually and importing the new dataset but I get this error
The mutation(s) failed: Cannot modify immutable attribute "_type"
AI Update

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:

The Sanity CLI migration tool is built for exactly this scenario:

npx sanity@latest migration create changeDocumentType

Then write a migration script that:

  1. Queries documents with the old _type
  2. Creates new documents with the new _type and copies all the content
  3. 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

  1. Document IDs: You can keep the same _id values if you delete the old documents first, or generate new IDs if you want to keep both temporarily
  2. References: If other documents reference these documents, those references will still work if you keep the same _id
  3. Drafts: Remember to handle both published documents and drafts (which have IDs prefixed with drafts.)
  4. 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.

Show original thread
48 replies

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.

Was this answer helpful?