Uploading same image multiple times in Sanity and getting different asset IDs.

13 replies
Last updated: Jun 7, 2022
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.

Append a timestamp or random number at the end
To filename? Filename changes every time. But if image buffer is exactly the same, it gets replaced.
if you add something like a timestamp to the file path, would it not be treated as a new image every time?
There is only the
filename
option which I update each time. I only attach the file buffer, which is exactly the same each time
If I do it from Sanity Studio, it works correctly
filename: 'avatar_'+res.locals.user._id+'_'+Date.now()+'.jpg
for example would that create a new file on each upload ?
Yes, just tried, it replaces old image with new name
I would of thought that would of made a new image entirely
I tried via Sanity Studio and I get this error:
An error occured: Asset already exists
Yea, me too, I tried adding multiple attributes to it too, like title, description, etc. But it’s same
I managed to upload two of the same images, one from server, one using Sanity Studio. But I cannot upload same image twice from same source. Either gets replaced, even if it has different name, or I get error:
An error occured: Asset already exists
The Studio actively prevents you from uploading the same image twice. From a structured content standpoint, it's better practice to upload a single high quality image and then use the Image URL API to request any variations on size and quality you may need.
What's your motivation for having multiple copies of the same image?
I agree, it doesn’t make sense to have same image twice.
Here’s my flow:
• I’m storing each user’s avatar at 300x300px on Sanity
• After upload, I’m storing a reference id to user’s document
• If user deletes his avatar, i’m getting their avatar id and delete it from Sanity
• If user B uploads same exact image as their avatar, they get same asset id as user A
• Now if they want to delete their avatar, I get an error, the image cannot be deleted because it’s referenced in another document.
This is a super rare case where two users have exact same image as their avatar and I wanted to just have different asset id’s for their own avatar.

I managed to make the backend not delete the image if it’s still referenced somewhere, so it’s all good.

Btw, studio lets you upload same image but with different resolutions, it has to have different buffer.

Thanks for the prompt support btw
🙌

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?