Images remain in old dataset after import without asset migration flag
Yes, this is expected behavior! When you export a dataset using sanity dataset export with the --raw flag and then import it to a different project ID or dataset name, you'll encounter this error.
Why This Happens
The imported documents would reference assets that exist outside of their own dataset. This creates a problematic situation because if you delete an asset from the source dataset, you'd end up with a "loose" asset document in the target dataset that points to a file that no longer exists.
The Solution
Don't use the --raw flag when exporting if you plan to import to a different dataset or project. Without --raw, the export will include all the assets from the source dataset, ensuring they're properly copied to the target dataset during import:
sanity dataset export production backup.tar.gz
sanity dataset import backup.tar.gz new-datasetThis way, your assets will be physically present in the new dataset rather than just referenced from the old one.
The Rare Exception
In very rare cases where you specifically want assets to reference URLs from a different dataset, you can use the --allow-assets-in-different-dataset flag when importing:
sanity dataset import backup.tar.gz new-dataset --allow-assets-in-different-datasetHowever, this is generally not recommended unless you have a specific use case that requires it, as it creates the dependency issue mentioned above.
For more details, check out the official documentation on this error and best practices for backing up and restoring datasets.
Show original thread29 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.