How to make a GraphQL mutation query that deletes an entry in Craft CMS

A step by step tutorial on using a GraphQL mutation query to delete an entry in Craft CMS using Postman.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 12/25/2022 at 11:21
Last Updated on 02/15/2023 at 14:00
Three flowers that represent Craft CMS, GraphQL & Postman side by side. Beneath them sits the text "GraphQL Mutations Delete an Entry."

A step by step tutorial on using a GraphQL mutation query to delete an entry in Craft CMS using Postman.

The following tutorial walks you through how to make a GraphQL mutation query that deletes an existing Subscriber in Craft CMS. It requires you to have a Craft CMS project setup to work with GraphQL, a valid token and schema as well as a channel to delete existing entries from.

SubscribeHow to make an API that makes GraphQL mutation queries in ServerlessHow to make a GraphQL mutation query that creates an entry in Craft CMSHow to make a GraphQL mutation query that edits an entry in Craft CMSDownload Open Source Craft CMS ProjectHow to create & setup a Craft CMS projectHow to create a channel in Craft CMSHow to setup Craft CMS to work with the GraphQLHow to create a GraphQL schema in Craft CMSHow to create a GraphQL token in Craft CMS

Step One: Determine the Schema of the entry

A screenshot of the Craft CMS GraphiQl demonstrating a query that gathers all the Subscribers.

Using the GraphiQL, which can be accessed at YOUR_CRAFTCMS_URL.com/admin/graphiql, determine the schema of the entry that you wish to create.

In our case, it looks similar to the one below

Please note that for mutations to be able to delete entries, under the "Choose the available mutations for this schema" section, your GraphQL schema must have checked off the "Delete entries with the “ENTRY_TYPE” entry type" under the section of interest and have a valid authorization bearer token tied to it.

How to create a GraphQL schema in Craft CMSHow to create a GraphQL token in Craft CMS

Step Two: Setup Postman to work with an Authorization Bearer Token

A screenshot of Postman with us having set the request type to POST and the URL to that of our GraphQL API.

Follow the tutorial below to set up Postman to work with an Authorization Bearer Token, ensuring that you have set the request to POST - which is required for saving entries.

How to make a GraphQL query with an Authorization Bearer Token in Postman

Step Three: Find the id

A screenshot of the Craft CMS GraphiQL with a highlight on the ID of the entry that we wish to delete.

Deleting entries with GraphQL in Craft CMS works by sending a "deleteEntry" mutation request to the server with the primary key (id) of the entry that you wish to delete.

To gather the entry id, we recommend that you make a GraphQL call that returns the id of the entry that you're looking for by narrowing down for the specific unique parameter (i.e. search for the email). We have attached the sample query that we used to do so below.

Step Four: Write your mutation query and variables

A screenshot of Postman with the completed deleteEntry mutation and variables.

Under the Body tab in the GraphQL section, create your delete mutation.

Please note that, as mentioned in Step Three, all delete mutations must implement an Id of an entry that you wish to delete.

To build your mutation, consider the sample mutation GraphQL query and associated GraphQL variables below:

A | Write the Mutation Query

A screenshot of Postman with the completed GraphQL mutation. Highlighted on the right is the GraphQL Query input box, which is where you must write the GraphQL mutation.

In Postman, the mutation query is written in the Query input of the GraphQL option in the Body tab.

A mutation starts with the word mutation followed by its title (deleteSubscriber) and then within brackets the id variable that you will use to delete the specific entry.

The Delete Entry GraphQL variables exclusively require you to use the id (i.e. $id: Int!) and must be included in the GraphQL variables that are used to delete the entry.

mutation deleteSubscriber($id: Int!)
How to filter for a Craft CMS site in a GraphQL Mutation

Subsequently, within the mutation query, you enter deleteEntry and in brackets (id: $id).

deleteEntry(id: $id)

B | Write the GraphQL Variables

A screenshot of Postman with the complete GraphQL Mutation for deleting entries. Highlighted on the right is the Query Variables input box, where you must write the GraphQL Variables associated with the mutation.

In Postman, the GraphQL variables are written in the GraphQL Variables input of the GraphQL option in the Body tab.

The GraphQL Variables are written in JSON format and are the datapoints that are used to create the entry in Craft CMS which must be tied to the $ parameters that you implemented when writing your mutation.

For every $ parameter that you implemented when writing your mutation, there must be a JSON parameter with a matching datapoint of the type associated to the parameter (i.e. $id: Int must be matched with "id": INTEGER).

Step Five: Test

A screenshot of Postman showing a successful response for a deleteEntry GraphQL mutation request.

When ready, press Send in Postman.

The response will appear at the bottom of the screen.

If you navigate to the section in Craft you will see that the entry was deleted.

A screenshot of Craft CMS showing all the subscribers before making the request.

Our Craft CMS Subscribers before making the GraphQL Mutation request.

A screenshot of Craft CMS showing all the subscribers after making the request. The mutation@sample.com, which was requested to be deleted has now disappeared.

Our Craft CMS Subscribers after making the GraphQL Mutation request.

The mutation@sample.com, has now disappeared, confirming the request succeeded.

Any Questions ?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe