Skip to content

Instantly share code, notes, and snippets.

@kmelve
Last active March 16, 2021 21:10
Show Gist options
  • Save kmelve/f719f7a9335f3fe470e0422ca654e54a to your computer and use it in GitHub Desktop.
Save kmelve/f719f7a9335f3fe470e0422ca654e54a to your computer and use it in GitHub Desktop.
Delete documents without corrensponding schema definition in a Sanity Studio folder
/**
* This script can be placed in the root of your studio folder,
* and be run with `sanity exec deleteDocsWithoutSchema.js --with-user-credentials
*/
import client from 'part:@sanity/base/client'
const getSanitySchema = require('./node_modules/@sanity/core/lib/actions/graphql/getSanitySchema')
const Schema = getSanitySchema(process.cwd())
const types = Schema._original.types.map(({name}) => name)
// Check which documents that currently haven't a corresponding schema in the studio
client.fetch('*[!(_type in $types)]', {types})
.then(result => console.log(result) || result)
/**
* You should probably export your dataset first. This permanently deletes data.
**/
// .then(result => {
// const ids = JSON.stringify(result)
// client.delete({query: `*[_id in ${ids}]`}).then(result => console.log('Deleted:', result)).catch(err => console.log(err))
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment