How to upload duplicate images with different IDs in Sanity?

13 replies
Last updated: Nov 29, 2025
Hello,I’m trying to upload assets via a node server. The problem is, if I upload same exact image, it doesn’t get uploaded, instead the old one gets replaced. How can I upload same image multiple times and have different id’s for each?


const imageAsset = await sanity*.*assets*.*upload('image', req*.*file*.*buffer, {

filename: 'avatar_' + res*.*locals*.*user*.*_id + '.jpg',

});
AI Update

This is actually expected behavior in Sanity - assets are automatically deduplicated based on their content hash. When you upload the same image multiple times, Sanity recognizes it's the same file and returns the existing asset ID rather than creating duplicates. This is by design to save storage space and prevent unnecessary duplication in your Content Lake.

From Sanity's documentation on uploading assets efficiently, it explicitly states: "Images uploaded to the Content Lake receive deterministic IDs based on the image content itself, ensuring that uploading the same image multiple times results in the same ID without creating duplicate documents."

Workarounds

If you truly need different asset IDs for the same image, you'll need to make the files technically different. Here are a few approaches:

1. Add metadata to the image buffer (recommended) Modify the image slightly before upload - even adding a single pixel or changing metadata will create a different hash:

const sharp = require('sharp');

const modifiedBuffer = await sharp(req.file.buffer)
  .resize() // This re-encodes the image
  .toBuffer();

const imageAsset = await sanity.assets.upload('image', modifiedBuffer, {
  filename: `avatar_${res.locals.user._id}_${Date.now()}.jpg`,
});

2. Add a timestamp or random data to the filename and image While changing just the filename won't affect deduplication (it's based on content hash), you could add a timestamp watermark or modify the image in some imperceptible way.

3. Reconsider your architecture The more common pattern is to embrace Sanity's deduplication and use references to the same asset from different documents. If you need to track different "versions" or "uses" of the same image, consider creating separate documents that reference the same asset rather than uploading duplicates.

Why This Design?

Sanity's deduplication is intentional and beneficial for most use cases:

  • Saves storage costs
  • Reduces CDN bandwidth
  • Prevents content bloat
  • Makes asset management cleaner

If you're using the same image for multiple user avatars, you might want to rethink whether you actually need separate asset documents, or if referencing the same asset from different user documents would work better for your use case.

Show original thread
13 replies

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.

Was this answer helpful?