0
0
GraphQLquery~5 mins

Delete mutation pattern in GraphQL

Choose your learning style9 modes available
Introduction

Deleting data helps keep your database clean by removing items you no longer need.

When a user wants to remove their account from a website.
When an admin needs to delete outdated or incorrect records.
When cleaning up temporary data after a process finishes.
When removing items from a shopping cart before checkout.
Syntax
GraphQL
mutation {
  deleteItem(id: "item-id") {
    id
    success
    message
  }
}

This is a basic GraphQL mutation to delete an item by its ID.

The response usually includes confirmation fields like success or message.

Examples
Deletes a user with ID 123 and returns confirmation.
GraphQL
mutation {
  deleteUser(id: "123") {
    id
    success
    message
  }
}
Deletes a post with ID 'abc' and returns success status.
GraphQL
mutation {
  deletePost(id: "abc") {
    id
    success
  }
}
Deletes a comment and returns success and message fields.
GraphQL
mutation {
  deleteComment(id: "cmt789") {
    success
    message
  }
}
Sample Program

This mutation deletes a book with ID 'book123' and returns the ID, success status, and a message.

GraphQL
mutation {
  deleteBook(id: "book123") {
    id
    success
    message
  }
}
OutputSuccess
Important Notes

Always check the response to confirm the deletion was successful.

Deleting data is permanent unless your system supports undo or soft deletes.

Make sure to have proper permissions before allowing delete mutations.

Summary

Delete mutations remove data by specifying an ID or unique identifier.

They usually return confirmation fields like success or message.

Use delete mutations carefully to avoid losing important data.