Level up your GraphQL + GraphDB knowledge - Register free for Dgraph Day
To optimize your experience, the interactive tutorial is only available on desktop.

Dgraph GraphQL Tour

Schema

Deleting Data

Dgraph’s generated GraphQL API also provides mutations to delete data from the graph. Data can be deleted in the following ways:

  • Update nodes and remove edges
  • Update nodes and remove predicate values
  • Delete a node with all of its inbound and outbound edges and predicate values.

To remove an edge from a node, you will need to use the same update mutation pattern that you saw in the previous lesson. Except this time, instead of using the set argument, you use the remove argument.

The following example query removes the “manages” and “manager” edges between Alice and Frank:

mutation {
  updatePerson(input: {
    filter: { xid: { eq: "alice" } }
    remove: { manages: [{ xid: "frank" }] }
  }) {
    person {
      xid
      manages {
        xid
      }
    }
  }
}
Note

The update<type> mutations let you set or remove multiple edges and values in a single operation.

The example mutation shown above removes edges between two specific nodes. It’s also possible to remove edges from one node to all other nodes. The following mutation example removes all nodes from the “manages” edge from Alice.

mutation {
  updatePerson(input: {
    filter: { xid: { eq: "alice" } }
    remove: { manages: null }
  })
}

Dgraph keeps the deletion of edges balanced (inclusive of both directions) when the @hasInverse directive is applied, just like it keeps edges balanced when creating them.

To delete predicate values, the syntax is very similar to the above. Instead of listing the edges to remove we can provide the value to remove. If the value is set to null, then all values are removed for that predicate.

mutation {
  updatePerson(input: {
    filter: { xid: { eq: "alice" } }
    remove: { age: null }
  }) {
    person {
      xid
      age
    }
  }
}

To delete a node with all of its predicate values, along with the inbound and outbound edges, we need to use the delete<type> mutation, as follows:

mutation {
  deletePerson(filter: {
    xid: "karl"
  }) {
    person {
      xid
      name
      age
    }
  }
}

The state of the nodes returned is the representation of the node before the deletion was applied, so the return value provides a final record of that data.

Note

When a node is deleted from Dgraph, the xid can be reused, but the GraphQL API will not allow the UID to be reused because all UIDs are set out of the control to the GraphQL API. It is possible to reuse UIDs if the GraphQL is bypassed and mutations are made from other endpoints.