Skip to content

How to replace an image referenced hundreds of times in Sanity?

8repliesLast updated: Nov 13, 2026

Hey all, is there a way to replace an image in dashboard, that is referenced hundreds of times?
AI Update

Your options depend on where the image lives. If your project uses Media Library, you can swap it from the UI with asset versions, no script needed. For a regular dataset image asset, upload the new image and run a content migration that changes every reference from the old asset ID to the new one.

Option 1: Media Library asset versions (no code)

Asset versions let you replace a Media Library asset everywhere it's used:

Things to know: you need a role that can update assets in Media Library. Versions are meant for variations of the same image (for example retouches or edits), not completely different images. Media Library is an Enterprise add-on.

Option 2: Content migration (dataset assets)

1. Upload the new image (in Studio or with client.assets.upload) and get both asset IDs, for example with this query in Vision:

*[_type == "sanity.imageAsset"]{_id, originalFilename, url}

2. Create a migration in your Studio folder:

npx sanity@latest migrations create "Replace image asset"

3. Repoint every reference. The object handler visits every nested object in every document, at any depth, so you don't need to know the field paths. Paths returned from a node handler are relative to that node (cheat sheet).

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

const OLD_ASSET_ID = 'image-<old-hash>-<width>x<height>-jpg'
const NEW_ASSET_ID = 'image-<new-hash>-<width>x<height>-jpg'

export default defineMigration({
  title: 'Replace image asset everywhere',
  // documentTypes: ['post', 'page'], // optional: limit to certain types
  migrate: {
    object(node) {
      if (node._ref === OLD_ASSET_ID) {
        return at(['_ref'], set(NEW_ASSET_ID))
      }
    },
  },
})

This changes only asset._ref. The image field's own crop and hotspot values stay as they are, so check they still fit the new image if its dimensions differ.

4. Dry-run, back up, then apply. migrations run is a dry run by default and prints the patches it would make. Add --no-dry-run to apply them (CLI reference):

npx sanity@latest migrations run <migration-id>               # dry run
npx sanity@latest datasets export production backup.tar.gz   # backup first
npx sanity@latest migrations run <migration-id> --no-dry-run

Migration changes can't be reversed in bulk, which is why the export comes first. If you hit rate limits, lower --concurrency (1–10, default 6).

5. Optionally delete the old asset. Content Lake rejects deleting an asset while any document still holds a strong reference to it, and the error names those documents (docs). Once the migration has run, delete the old asset document by its _id.

Why not just overwrite the file?

Dataset assets are deduplicated by content hash, and the hash is part of the asset ID. A different file always gets a different asset ID, so you have to update the references (Option 2) or use Media Library versions (Option 1).

For more on the tooling, see schema and content migrations.

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