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

Oscar de la Hera Gomez
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.

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.

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.

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!)

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.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details