godgraphdgraph-dgo

How to delete quad in dgraph using dgo api.NQuad


Is it possible to remove all edges matching a predicate from a given node using api.NQuad from github.com/dgraph-io/dgo/protos/api?

I'm trying to achieve the equivalent of delete {0x1234 <test.likes> * }

func TestDeleteQuad(t *testing.T) {
    subject := "0x01"
    ctx := context.TODO()
    d, err := grpc.Dial(testEndpoint, grpc.WithInsecure())
    if err != nil {
        panic(err)
    }
    client := dgo.NewDgraphClient(api.NewDgraphClient(d))
    txn := client.NewTxn()
    defer txn.Discard(ctx)
    if _, err := txn.Mutate(ctx, &api.Mutation{Del: []*api.NQuad{
        &api.NQuad{
            Subject:     subject,
            Predicate:   "test.likes",
            ObjectId:    "*",
            ObjectValue: nil,
        },
    }}); nil != err {
        panic(err)
    }
    err = txn.Commit(ctx)
    assert.NoError(t, err)
}

I tried using "*" "" x.Star as ObjectId but none of those solutions work


Solution

  • This is quite counter intuitive, but to delete edges, ObjectValue must be used instead of ObjectId has to be set to api.Value_DefaultVal star:

    func TestDeleteQuad(t *testing.T) {
        subject := "0x01"
        ctx := context.TODO()
        d, err := grpc.Dial(testEndpoint, grpc.WithInsecure())
        if err != nil {
            panic(err)
        }
        client := dgo.NewDgraphClient(api.NewDgraphClient(d))
        txn := client.NewTxn()
        defer txn.Discard(ctx)
        if _, err := txn.Mutate(ctx, &api.Mutation{Del: []*api.NQuad{
            &api.NQuad{
                Subject:     subject,
                Predicate:   "test.likes",
                ObjectValue: &api.Value{&api.Value_DefaultVal{x.Star}}, // <- this
            },
        }}); nil != err {
            panic(err)
        }
        err = txn.Commit(ctx)
        assert.NoError(t, err)
    }