Delete Edges in Dgraph v0.9/v1.0

Dear all - first post here, pardon my ignorance if it’s already been covered somewhere.
In earlier versions of dgraph, I was able to use the following delete syntax (using the golang GRPC client) when I needed to update a relationship to point to a different entity.

In the example below, I changed the household a person belongs to by first deleting the present edge, then assigning a new UID value to the “person.household” edge.

	deleteCursor = newPersonNode.Edge("person.household")
	_ = deleteCursor.Delete()
	_ = dgraphReq.Delete(deleteCursor)

Which translates to the following in RDF

  delete {
     <0x3669> <person.household> <0x366a> .

Is there an equivalent way of accomplishing the same in the newer JSON-based dgraph golang API? Based on the DeleteNode tutorial, it seems that the mu.DeleteJson does the deletion based on the UID of the deleted node, however it’s not clear to me that the same could be accomplished for deleting an edge/relationship.

Thank you!

If you just want to update person.household to point to a different node, you can just overwrite it directly (no need to delete first).

If you really want to delete though, you can use the DeleteJson field in the Mutation object. I’m not sure of the structure of the JSON object you would need to provide, @pawan can provide you with more guidance here.

By the way, you can always fall back to the old RDF style mutations, by using the SetNquads and DelNquads fields in the Mutation object. These can be a bit easier to understand if you’re already familiar with Nquads, although they can lead to more verbose code compared to using JSON.

Hi @zhongliangong

You can use the DeleteEdges for this before adding the new person.household.

mu = &api.Mutation{}
client.DeleteEdges(mu, 0x3669, "person.household")

mu.CommitNow = true
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
1 Like

Thank you @pawan - Just what I needed! (and thank you @peter - would have fallen back to RDF if it didn’t work).
Thank you all for your quick replies even though I’m sure you’ve got a long laundry list of features to crank out.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.