GraphQL Cookbook

Deleting an object through the Parse GraphQL API

Problem

You want to delete an existing object in your database through the Parse GraphQL API.

Solution

Using the parse GraphQL, there are two different ways to delete an existing object in your database:

  • Using generic mutation - this is the mutation that you can use to delete an object of any class.
  • Using class mutation - this is the recommended mutation that you should use to delete an object of a specific class.

Using generic mutation

When you use the delete generic mutation, you send the object’s className and objectId, and Parse Server will delete this object.

Therefore, the objects’ delete generic mutation is the one that you can use for deleting an existing object of any class. If you want to delete an existing object of a specific class, we recommend using the class mutation.

Example:

This example will only work if you use a className and an objectId of an existing object. You can create an object using the creating an object recipe.

1
2
3
4
5
mutation DeleteObject {
  objects {
    delete(className: "Hero", objectId: "ffyOBOTk85")
  }
}
Result:
1
2
3
4
5
6
7
{
  "data": {
    "objects": {
      "delete": true
    }
  }
}

Using class mutation

Once you have already created your object’s class in your application’s schema (for instance, using the creating an object recipe), Parse Server instantly adds to your GraphQL API a new delete<ClassName> mutation to delete an existing object of this class.

Therefore, the object’s class mutation is the recommended method for deleting an existing object of a specific class.

Example:

This example will only work if you use a class’ mutation and objectId of an existing object. You can create an object using the creating an object recipe.

1
2
3
4
5
mutation DeleteHero {
  objects {
    deleteHero(objectId: "jJH0aQQjfs")
  }
}
Result:
1
2
3
4
5
6
7
{
  "data": {
    "objects": {
      "deleteHero": true
    }
  }
}