Joint session with Vercel: How to build intelligent storefronts (May 15th)

How to change _type of existing documents in Sanity?

48 repliesLast updated: Nov 29, 2025

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:

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

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

Was this answer helpful?

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.

Related contributions