🗓️ Everything *[NYC] is back. A free gathering for AI builders. Sept 9 →

Best way to batch remove deprecated field from Sanity dataset

22 repliesLast updated: Nov 29, 2025

Hi,I'm looking for a efficient way to remove a deprecated field from a Dataset. I have tried executing this
script and tried with a migration script with an unset(). I not having any success atm. What is the best practice when you want to batch remove a field from a dataset.
AI Update

Looking at your situation, the recommended approach for batch removing a deprecated field from your dataset is to use Sanity's migration tooling with the unset() function. Let me walk you through the best practice approach.

Using defineMigration with unset()

First, create a migration script:

npx sanity@latest migration create "Remove deprecated field"

Then in the generated migrations/your-migration-name/index.ts file, use this pattern:

import {defineMigration, at, unset} from 'sanity/migrate'

export default defineMigration({
  title: 'Remove deprecated field',
  documentTypes: ['yourDocumentType'], // specify the document type(s)
  
  migrate: {
    document(doc, context) {
      // Remove the deprecated field
      return at('deprecatedFieldName', unset())
    }
  }
})

Running the Migration

The key to success is following this process:

sanity migration run your-migration-name

This shows you what would happen without making actual changes.

sanity migration run your-migration-name --dataset <your-dataset-name>

Why This Approach Works

The defineMigration API handles several important things automatically:

Removing Multiple Fields

If you need to remove multiple deprecated fields at once:

migrate: {
  document(doc, context) {
    return [
      at('deprecatedField1', unset()),
      at('deprecatedField2', unset()),
      at('nested.deprecatedField', unset())
    ]
  }
}

Troubleshooting Tips

If you're still not seeing success:

The migration tooling is specifically designed for this batch patch use case and handles all the complexity of batching mutations automatically, making it more reliable than manual patching scripts. If you're still experiencing issues after following this pattern, double-check that your field names and document types exactly match what's in your dataset.

Show original thread
22 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