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

How to bulk generate slugs for existing documents in Sanity?

6 repliesLast updated: Dec 1, 2025

I have tons of articles, but they don't have slugs. I've now added a slug field to the schema, but it looks like I have to manually go in to every article, click generate, and then publish.
Is there a quicker way?
😄
AI Update

Great question! Yes, there's definitely a better way than manually clicking through every article. You can use Sanity's migration tooling to bulk-generate slugs for all your existing articles.

Create a Migration Script

First, generate a new migration using the CLI:

npx sanity@latest migration create "generate slugs for articles"

This will create a migration file in your migrations directory. Open the generated index.ts file and replace it with something like this:

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

// Helper function to create URL-friendly slugs
function slugify(text: string): string {
  return text
    .toLowerCase()
    .replace(/[^\w\s-]/g, '') // Remove special characters
    .replace(/\s+/g, '-')      // Replace spaces with hyphens
    .replace(/--+/g, '-')      // Replace multiple hyphens with single
    .trim()
}

export default defineMigration({
  title: 'Generate slugs for articles',
  documentTypes: ['article'], // Replace with your document type
  
  migrate: {
    document(doc, context) {
      // Only generate if there's a title and slug is missing
      if (doc.title && !doc.slug?.current) {
        return [
          at('slug', setIfMissing({
            _type: 'slug',
            current: slugify(doc.title)
          }))
        ]
      }
      return []
    }
  }
})

Test It First (Dry Run)

Before making any changes, run it in dry-run mode to see what would happen:

npx sanity migration run generate-slugs-for-articles

This won't actually change anything - it just shows you what would be updated. Review the output to make sure it looks good!

Run the Migration

Once you're happy with the dry run results, execute it for real:

npx sanity migration run generate-slugs-for-articles --dataset production

Replace production with your dataset name (often production or development).

The migration toolkit automatically handles batching, rate limits, and gives you progress feedback. The setIfMissing operation ensures you won't overwrite any slugs that already exist, so it's safe to run even if some articles already have slugs.

Important Notes

This approach is way better than clicking through hundreds of articles! 😄

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