-
-
Save mindmergedesign/7d840291a7035afebad2c73630671f40 to your computer and use it in GitHub Desktop.
Delete documents without corrensponding schema definition in a Sanity Studio folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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