DEV Community

Julian Bustos
Julian Bustos

Posted on

How to duplicate a Sanity Dataset for FREE

Updating a website's backend can be nerve-wracking, especially when the site is live. Introducing new features often requires code changes, and there's always a risk of something breaking unintentionally. This article explores a cost-effective way to avoid disrupting your Sanity CMS backed site during development by creating a separate development dataset.

Have you ever screwed up your own site, because you were working on something else?

I have a couple of times, and sometimes It's understandable because you are working on fixing it and no one is looking at it. Sometimes it's more important.

I am at that point where it matters, my client wants to start updating the backend with the content, and I'm trying to write code to implement the latest features requested.

An update that for a small bit of time would break the site. The good news is, it doesn't have to. If we split our datasets and we do our development on another dataset we can avoid issues like these. But I also want all the content that's already on the site.

When you look up the Sanity documentation you'll come across a very handy option to copy the dataset into another dataset, but when you run it you this functionality requires a paid plan. However, you could do this for free too if we export the dataset and then import it into the new dataset, just a couple more steps.

  • Check sanity version or install a the cli tool.
npm install --global sanity@latest

# Alternatively
yarn global add sanity@latest
pnpm install --global sanity@latest

# Running the CLI without global installation
npx -y sanity@latest [command]
Enter fullscreen mode Exit fullscreen mode
  • Login to your Sanity account using the cli tool.
sanity login 
Enter fullscreen mode Exit fullscreen mode
  • Remember to login with the correct user 😅.

  • Setup your cli env.

  • In the root of your project create a file called sanity.cli.ts or sanity.cli.js with the following content:

// sanity.cli.ts
import { defineCliConfig } from "sanity/cli";

export default defineCliConfig({
  api: {
    projectId: "<your-project-id>",
  }
});
Enter fullscreen mode Exit fullscreen mode
  • cd into the root of your project
  • Export your dataset
sanity dataset export <dataset-name> <export-file.tar.gz> [--types <type1,type2,...>]
Enter fullscreen mode Exit fullscreen mode
  • dataset-name: Replace this with the actual name of the dataset you want to export (e.g., "production").
  • export-file.tar.gz: Specify the desired filename and location for the exported data archive (e.g., "my-data.tar.gz").
  • --types (optional): This flag allows you to export specific document types only. Separate multiple types with commas (e.g., "--types post, product").

in my case this means:

sanity dataset export production production-file.tar.gz
Enter fullscreen mode Exit fullscreen mode

when its done we can now create the new dataset.

sanity dataset create development
Enter fullscreen mode Exit fullscreen mode
  • select your choice, for me this is a public dataset as it's going to be used for development only.

  • import your downloaded file into your dataset

sanity dataset import [FILE | FOLDER | URL] [TARGET_DATASET]
Enter fullscreen mode Exit fullscreen mode

for me that's going to be:

sanity dataset import production-file.tar.gz development
Enter fullscreen mode Exit fullscreen mode

Once done you have duplicated or copied your dataset for FREE!

  • Setup a SANITY_DATASET environment variable in your env file.
//.env.local
SANITY_DATASET=development
Enter fullscreen mode Exit fullscreen mode
  • Update your sanity config file.
// sanity.config.ts
export const studioConfig = defineConfig({
  //...rest of your config
  dataset: process.env.SANITY_DATASET || "",
})
Enter fullscreen mode Exit fullscreen mode
  • If you have a deployed version of the site you will want to update the environment variables of your deployed site.

  • You probably will want to delete that file from your project root, or store it somewhere else.

Thank you for reading!

Top comments (0)