Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to replace an image in a dashboard referenced hundreds of times using a migration script in Sanity.io

8 replies
Last updated: Sep 8, 2022
Hey all, is there a way to replace an image in dashboard, that is referenced hundreds of times?
Jul 21, 2022, 1:16 PM
Not without a migration script to do so.
Jul 21, 2022, 1:32 PM
Can you give me a starter on that please?
Jul 21, 2022, 5:28 PM
You'll need to run a patch on any document that contains a reference to that particular asset.
Jul 21, 2022, 5:32 PM
This can also be done using the JS Client if you prefer to write a script that you run using
sanity exec
. This is an example I previously shared:
import { studioClient } from './studioClient';
import cq from 'concurrent-queue';

// Create a queue to limit the rate at which you write changes to Sanity
let queue = cq()
  .limit({ concurrency: 2 })
  .process(task => {
    return new Promise(function (resolve, reject) {
      setTimeout(resolve.bind(undefined, task), 1000);
    });
  });

const mutateDocs = async () => {
  //Fetch the documents you need to mutate
  const query = `*[<your-query>]`;
  const docs = await studioClient.fetch(query);
  // Loop through all of the docs returned from our query
  for (const doc of docs) {
    queue(doc).then(async () => {
      // Add a message to help us know the upload is happening
      console.log(`Mutating ${doc._id}`);
      // Tell the client to patch the current document
      studioClient
        .patch(doc._id)
        // Set the field
        .set({
          //your changes here
        })
        // Commit the changes
        .commit()
        .then((updatedDoc) =>
          console.log(`Hurray, the doc is updated! New document:`, updatedDoc)
        )
        .catch((err) =>
          console.error('Oh no, the update failed: ', err.message)
        );
    // });
  }
};

mutateDocs();

// execute this script by running
// $ sanity exec ./lib/utils/mutateDocs.js --withUserToken
Jul 21, 2022, 5:35 PM
Great, thanks. Do you know if I can keep the hot spots this way?
Jul 21, 2022, 6:12 PM
You can, but you have to explicitly set it. Just remember to take the asset's crop and hot spot and apply it to the new one in your patch.
Jul 21, 2022, 7:26 PM
Thanks, I'll look into this.
Jul 21, 2022, 7:40 PM
user M
Once again saving my bum with some deep-dive research here to alleviate what I ran into here https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1661800253439929
I recognize that CQ is a separate package, and throttled to 15 requests a second (nice to know nothing else waits on the updates because it takes a few seconds at a hundred plus docs) but now my default sort ordering listens to an invented and patched field I made!
Sep 8, 2022, 3:42 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?