Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

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
Apr 9, 2021, 7:49 AM
https://www.sanity.io/docs/documents can delete them from the cli
Apr 9, 2021, 7:52 AM
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>
Apr 9, 2021, 7:52 AM
Hi
user M
am trying to delete by id but this error occuring,
Apr 9, 2021, 7:57 AM
even though doc exist
Apr 9, 2021, 7:58 AM
is you cli looking at the prod dataset?
Apr 9, 2021, 7:58 AM
well, it is on prod dataset but how wil iknow that cli is hittin the right dataset?
Apr 9, 2021, 7:59 AM
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.
Apr 9, 2021, 8:02 AM
tried with
_id
but now facing this error,
user M
Apr 9, 2021, 8:04 AM
ohh i was missing parantheses, in command, now deleted successfully,
Apr 9, 2021, 8:07 AM
Thanks
user M
Apr 9, 2021, 8:08 AM
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.
Apr 9, 2021, 8:08 AM
Ah, alright, if that does the trick that's great I guess šŸ™‚ Glad to hear!
Apr 9, 2021, 8:08 AM
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.
Apr 9, 2021, 8:11 AM
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>")]
Apr 9, 2021, 8:12 AM
In your CLI:
sanity documents query '*[references("<idOfDocumentYouAreTryingToDelete>")]'
Apr 9, 2021, 8:14 AM
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

}

]
Apr 9, 2021, 8:17 AM
tried to delete with both _ref &amp; _id but no success
Apr 9, 2021, 8:19 AM
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.
Apr 9, 2021, 8:20 AM
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 šŸ™‚
Apr 9, 2021, 8:22 AM
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"

}

]
Apr 9, 2021, 8:22 AM
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
Apr 9, 2021, 8:24 AM
Sorry about all the references here šŸ™‚ Can be quite confusing. Are you able to see this
navigationMenu
doc in the studio itself?
Apr 9, 2021, 8:25 AM
No, Peter i can't
navigationMenu
see anywhere on studio, not even sure where it comes from
Apr 9, 2021, 8:27 AM
What's under "Navigations" in your studio?
Apr 9, 2021, 8:27 AM
Btw, let me clarify you, there is another branch, in which this
Navigations
doc, doesn't exist but still facing this issue,
Apr 9, 2021, 8:29 AM
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?
Apr 9, 2021, 8:30 AM
And you're not using the actual
navigationMenu
and
route
setup yourself at this point on the front-end side?
Apr 9, 2021, 8:30 AM
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?
Apr 9, 2021, 8:31 AM
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
Apr 9, 2021, 8:32 AM
Hi
user M
i ve deleted all docuents, but now i am unable to create a new one, šŸ˜„
Apr 9, 2021, 9:13 AM
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?
Apr 9, 2021, 9:27 AM
yes, page schema is added correctly on
schema.js
and this is the error i got,
Apr 9, 2021, 9:42 AM
Could you share your
page.js
schema definitions? There may be an issue with the
initialValue
setup in there.
Apr 9, 2021, 9:46 AM
// 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;

});

},

},

],

};
Apr 9, 2021, 9:52 AM
// 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"),

])

),

]);
Apr 9, 2021, 9:53 AM
// 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,

]),

})
Apr 9, 2021, 9:54 AM
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())
  ),
Apr 9, 2021, 10:01 AM
ok, let me try with it,
Apr 9, 2021, 10:03 AM
wait, now it's working,
Apr 9, 2021, 10:05 AM
Thanks
user M
but what excatly is ths issue, and what was the fix? can you tell me a bit about it?
Apr 9, 2021, 10:05 AM
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.
Apr 9, 2021, 10:10 AM
ok and what's this line of code for?

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

Apr 9, 2021, 10:11 AM
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.
Apr 9, 2021, 10:13 AM
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"
Apr 9, 2021, 10:14 AM
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?
Apr 9, 2021, 10:22 AM
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.
Apr 9, 2021, 10:30 AM
fine
user M
thank you very much for your time.
Apr 9, 2021, 10:34 AM
Very welcome - looking forward to seeing what you'll build! šŸ™‚
Apr 9, 2021, 10:35 AM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?