Skip to content

Instantly share code, notes, and snippets.

@good-idea
Created February 9, 2020 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save good-idea/1abc5429c0c2a0be760d3a318468c750 to your computer and use it in GitHub Desktop.
Save good-idea/1abc5429c0c2a0be760d3a318468c750 to your computer and use it in GitHub Desktop.
Remove all shopifyProducts and shopifyCollections from sanity dataset
/**
WARNING: THIS DELETES DATA
Run this within your Sanity project directory with:
yarn sanity exec --with-user-token removeSaneShopifyDocuments.js
**/
import client from 'part:@sanity/base/client'
const query =
'*[_type == "shopifyProduct" || _type == "shopifyCollection"]{ _id }'
const fetchDocuments = async () => client.fetch(query)
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const createTransaction = (patches) =>
patches.reduce(
(tx, patch) => tx.patch(patch.id, patch.patch),
client.transaction(),
)
const commitTransaction = (tx) => tx.commit()
const run = async () => {
const docs = await fetchDocuments()
const patches = docs.map((doc) => ({
id: doc._id,
patch: {
unset: ['collections', 'products'],
},
}))
const unsetTx = createTransaction(patches)
const r = await unsetTx.commit()
const d = await client.delete({ query })
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment