Deleting pages in Sanity and resolving related reference issues

48 replies
Last updated: Apr 9, 2021
Hello there, how can i delete these Home and About pages from sanity, i am unable to do it, due to this error
AI Update

This is a common issue in Sanity! You're getting this error because the Home and About pages you're trying to delete are referenced by other documents in your content. Sanity prevents you from deleting documents that have strong references pointing to them - this is a security feature that protects your content integrity.

Finding Which Documents Are Referencing Your Pages

The easiest way to find what's referencing your pages is to use Vision (Sanity's GROQ playground):

  1. Open Vision in your Studio (usually accessible from the top menu or at /vision)
  2. Run this query to find all documents referencing your page:
*[references("YOUR_DOCUMENT_ID")]

Replace YOUR_DOCUMENT_ID with the actual _id of your Home or About page. You can find the document ID by opening the document in Studio and checking the "Inspect" panel.

Alternatively, you can use this more detailed query that shows you which documents have references:

*[]{_id, title, "references": *[references(^._id)]}[length(references) > 0]

This will show you all documents and what's referencing them.

Removing the References

Once you know which documents are referencing your pages, you have several options:

  1. Open those referencing documents in Studio and remove or change the reference fields pointing to the pages you want to delete
  2. Delete the referencing documents first (if they're not needed)
  3. Unset the reference - find the links to these documents in the Studio UI and delete them

Based on discussions in the Sanity community, common places where references hide include:

  • Navigation menus or site configuration documents
  • Other pages that link to Home/About
  • Array fields containing page references
  • Sometimes orphaned references from integrations (like your Shopify case)

Making References "Weak" (Future Prevention)

If you want to allow deletion even when references exist, you can modify your schema to use weak references:

{
  name: 'pageReference',
  type: 'reference',
  weak: true, // This allows the referenced document to be deleted
  to: [{type: 'page'}]
}

With weak references, Sanity won't block deletion - though the referencing documents will have broken links.

The Vision query is your best tool here for tracking down exactly what's holding onto those references. Once you clean those up, you'll be able to delete the Home and About pages without issues!

https://www.sanity.io/docs/documents can delete them from the cli
Hi
user R
, you could temporarily re-create a
page
document type to interact with these documents or remove them via the command line instead:
sanity documents delete <_id1> <_id2>
Hi
user M
am trying to delete by id but this error occuring,
even though doc exist
is you cli looking at the prod dataset?
well, it is on prod dataset but how wil iknow that cli is hittin the right dataset?
Looks like you may be getting the
id
from Gatsby's GraphiQL, which is not precisely the same as the
_id
in Sanity. Sorry about the confusion there 🙂 Could you try using the
_id
(notice the underscore) from the first screenshot you shared, so inside of the studio?
FYI it's also available as
_id
in GraphiQL if you prefer that approach.
tried with
_id
but now facing this error,
user M
ohh i was missing parantheses, in command, now deleted successfully,
Thanks
user M
Looks like it has a so-called "strong" reference to it, which prevents it from being deleted here as a measure to not end up with broken references. There are several ways around this, but the easiest is probably to open up the relevant doc and unset the reference. You could copy the second ID in the studio search to find the doc.
Ah, alright, if that does the trick that's great I guess 🙂 Glad to hear!
Ohh, the other one was deleted succesfully, but first one which is home page has same issue occuring, now i am not sure where this is referencing, as i have't create any ref.
Are you getting the ID in the error message? If so, you could paste it into the studio search bar to find the document that references it. Alternatively, you could run a query like:
*[references("<idOfDocumentYouAreTryingToDelete>")]
In your CLI:
sanity documents query '*[references("<idOfDocumentYouAreTryingToDelete>")]'
user M
ive ran the above query and this is the result i got.

[

{

"_createdAt": "2021-04-07T06:00:07Z",

"_id": "94e1b8ef-ac28-4ead-ba44-6d9d716799fa",

"_rev": "p5oiLzuoOfR9bp21r89iBt",

"_type": "route",

"_updatedAt": "2021-04-07T06:00:40Z",

"page": {

"_ref": "a4921de5-35b0-42e6-9505-c975ff2375c7",

"_type": "reference"

},

"slug": {

"_type": "slug",

"current": "home-page"

},

"useSiteTitle": false

}

]
tried to delete with both _ref &amp; _id but no success
Are you using
route
documents in your setup? For example under "Navigations" in your studio? If so, you should be able to unset the reference there and then proceed to delete the page document.
Alternatively, you could first delete
94e1b8ef-ac28-4ead-ba44-6d9d716799fa
(the route doc) and then the page document. However, you may have to re-create the
home-page
route afterwards if it your web front-end relies on it 🙂
No, i didn't create any
route
doc, but i think its coming from draft,like i ran
sanity documents query '*[references("94e1b8ef-ac28-4ead-ba44-6d9d716799fa")]'
and got this result,

[

{

"_createdAt": "2021-04-07T05:58:28Z",

"_id": "drafts.6ab68685-c225-4ddb-b362-00b5b0c8e6a0",

"_rev": "vwwzyg-8pq-7eo-hhv-uufhdc7m6",

"_type": "navigationMenu",

"_updatedAt": "2021-04-07T06:27:50Z",

"items": [

{

"_key": "6ef39e263942",

"_type": "cta",

"kind": "link",

"route": "/about",

"title": "about page"

},

{

"_key": "b9b73e0ac76d",

"_type": "cta",

"kind": "link",

"landingPageRoute": {

"_ref": "94e1b8ef-ac28-4ead-ba44-6d9d716799fa",

"_type": "reference"

},

"route": "/",

"title": "home page"

}

],

"title": "Nav menu"

}

]
yes ,i tried to delete
94e1b8ef-ac28-4ead-ba44-6d9d716799fa
but it itself ref to some thing, probably this,
drafts.6ab68685-c225-4ddb-b362-00b5b0c8e6a0
Sorry about all the references here 🙂 Can be quite confusing. Are you able to see this
navigationMenu
doc in the studio itself?
No, Peter i can't
navigationMenu
see anywhere on studio, not even sure where it comes from
What's under "Navigations" in your studio?
Btw, let me clarify you, there is another branch, in which this
Navigations
doc, doesn't exist but still facing this issue,
Ah, I'm suspecting you kicked off this project from a starter project on sanity.io/create and then restructured it to fit your needs? But there's some left-over data that you'd like to get rid of?
And you're not using the actual
navigationMenu
and
route
setup yourself at this point on the front-end side?
the problem is that i didn't created this project myself, and this issue started occuring from yesterday. So is there anyway to cleaned this up?
Could you try running these in this order:
sanity documents delete drafts.6ab68685-c225-4ddb-b362-00b5b0c8e6a0 6ab68685-c225-4ddb-b362-00b5b0c8e6a0

sanity documents delete 94e1b8ef-ac28-4ead-ba44-6d9d716799fa

sanity documents delete a4921de5-35b0-42e6-9505-c975ff2375c7
Hi
user M
i ve deleted all docuents, but now i am unable to create a new one, 😥
Could you check your dev tools console for extra details on that error? Also, have you double-checked that the
page
document type is included in your
schema.js
file?
yes, page schema is added correctly on
schema.js
and this is the error i got,
Could you share your
page.js
schema definitions? There may be an issue with the
initialValue
setup in there.
// pages.js


export default {

name: "page",

type: "document",

title: "Pages",

fields: [

{

name: "title",

title: "Title",

type: "string",

description: "An internal description of the page",

},

{

name: "metaTitle",

title: "Meta Title",

type: "string",

description: "Text for the title tag",

},

{

name: "metaDescription",

title: "Meta Description, as used as summary text for story cards",

type: "string",

},

{

name: "slug",

title: "Slug",

type: "slug",

options: {

source: "metaTitle",

slugify: async (_value_, _type_) => {

const { default: slugify } = await import("slugify");

return slugify(_value_, {

lower: true,

});

},

},

validation: (_Rule_) => {

return _Rule_.required().custom((_slug_) => {

if (_slug_ && _slug_.current.indexOf(" ") > -1) {

return "Slugs cannot have spaces";

}

// if (slug && slug.current.indexOf("/") > -1) {

//   return "Slugs cannot have slashes";

// }

return true;

});

},

},

],

};
// deskStructure.js


import S from "@sanity/desk-tool/structure-builder";

import { GoListUnordered } from "react-icons/go";

import { GiBookshelf, GiBookCover, GiFootTrip } from "react-icons/gi";

import { FiNavigation, FiSettings } from "react-icons/fi";

import { FaTags } from "react-icons/fa";

import {

RiPagesLine,

RiNavigationLine,

RiFootprintLine,

RiPagesFill,

} from "react-icons/ri";

import { AiFillAlert } from "react-icons/ai";


export default () =>

S.list()

.title("Content")

.items([

S.listItem()

.title("Site Settings")

.icon(FiSettings)

.child(

S.list()

.title("Settings")

.items([

...S.documentTypeListItems().filter((_item_) => {

const id = _item_.getId();

return ["siteSettings"].some((_type_) => _type_ === id);

}),

S.divider(),

S.documentTypeListItem("headerLink").title("Header Link"),

S.documentTypeListItem("footerLink").title("Footer Link"),

S.documentTypeListItem("sideMenuLink").title("sideMenuLink"),

])

),

S.listItem()

.title("Pages")

.icon(RiPagesLine)

.child(

S.documentList("page")

.title("Pages")

.menuItems(S.documentTypeList("page").getMenuItems())

.filter('_type == "page"')

.child((_documentId_) =>

S.document()

.documentId(_documentId_)

.schemaType("   ")

.views([S.view.form()])

)

),

S.listItem()

.title("Navigations")

.icon(FiNavigation)

.child(

S.list()

.title("Navigations")

.items([

...S.documentTypeListItems().filter((_item_) =>

["topNav"].some((_type_) => _type_ === _item_.getId())

),

S.divider(),

...S.documentTypeListItems().filter((_item_) =>

["navigation"].some((_type_) => _type_ === _item_.getId())

),

S.divider(),

// S.documentTypeListItem("topNav").title("Top Navigations Links"),

// S.documentTypeListItem("navigation").title("Navigation Links"),

])

),

]);
// schema.js




// First, we must import the schema creator

import createSchema from 'part:@sanity/base/schema-creator'


// Then import schema types from any plugins that might expose them

import schemaTypes from 'all:part:@sanity/base/schema-type'


// document schemas

import siteSettings from './documents/siteSettings'

import pages from './documents/pages';

import navigation from './documents/navigations/navigation';

import topNavigations from './documents/navigations/topNavigations';


import footerLink from './footerLink';

import sideMenuLink from './sideMenuLink';

import headerLink from './headerLink';

import selectFooter from './selectFooter';

import selectSideMenu from './selectSideMenu';

import selectHeader from './selectHeader';



// Object types

import link from './objects/link';

import navItem from './objects/navItem';

import reserveBtn from './objects/reserveBtn';


// Then we give our schema to the builder and provide the result to Sanity

export default createSchema({

// We name our schema

name: 'default',

// Then proceed to concatenate our document type

// to the ones provided by any plugins that are installed

types: schemaTypes.concat([

/* Your types here! */

footerLink,

sideMenuLink,

headerLink,

selectFooter,

selectSideMenu,

selectHeader,

siteSettings,

pages,


topNavigations,

navigation,

navItem,

link,

]),

})
Thanks
user R
. Could you try replacing this part of your
deskStructure.js
file:
S.listItem()
  .title("Pages")
  .icon(RiPagesLine)
  .child(
    S.documentList("page")
      .title("Pages")
      .menuItems(S.documentTypeList("page").getMenuItems())
      .filter('_type == "page"')
      .child((documentId) =>
        S.document()
          .documentId(documentId)
          .schemaType("   ")
          .views([S.view.form()])
      )
  ),
With this instead:

S.listItem()
  .title("Pages")
  .icon(RiPagesLine)
  .child(
    S.documentList("page")
      .title('Pages')
      .menuItems(S.documentTypeList('page').getMenuItems())
      .filter('_type == "page"')
      .canHandleIntent(S.documentTypeList('page').getCanHandleIntent())
  ),
ok, let me try with it,
wait, now it's working,
Thanks
user M
but what excatly is ths issue, and what was the fix? can you tell me a bit about it?
There was an empty
.schemaType("   ")
in the original - it might also have worked if you left it intact like it was but with the added
.schemaType("page")
instead. The above is a bit shorter as you don't necessarily need the extra child unless you want to customise views for example.
ok and what's this line of code for?

.canHandleIntent(S.documentTypeList('page').getCanHandleIntent())

That line ensures that you can use the "compose" icon to create a new document right inside the custom desk structure you define in
deskStructure.js
. So it won't collapse the panes but instead opens the new document in the right place.
It says: "this item in my desk structure can handle the intents [e.g. create or edit] for the page document type, so these intents will now open here"
Thanks Very much
user M
, ok another thing i want to ask is that i was exploring kitchen sink studio and could not get what's the difference between normal and kitchen sink studio? an how can i make this studio to kitchen sink studio?
The kitchen sink is a normal studio but just has a lot of stuff built into it. It serves as an example of some of the things you can do with the Sanity studio, in the same way as other starters do on sanity.io/create . You can run a starter as an example project or look at the GitHub repositories for hints on how to build parts of it into your own studio.
fine
user M
thank you very much for your time.
Very welcome - looking forward to seeing what you'll build! 🙂

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.

Was this answer helpful?