Feature request: detached delete for node

pawanrawal commented :

Had a discussion with @manishrjain about this.

I’d like to point out that the reason a normal detached delete isn’t supported right now is that given how data is stored internally in Dgraph, to execute a detached delete

  1. We would need to do a full database scan to find all <subject, predicate> combinations which contain an object (uid) and rewrite data on disk. Hence, this operation won’t scale very well as the size of the database grows.
  2. We’d also have to somehow make sure that all the count indexes are still up to date and have the correct data which is another expensive operation.

A way to get around the problem of ghost nodes while querying would be to use the GraphQL layer of Dgraph (https://graphql.dgraph.io) which ensures that the nodes being returned have the type specified in the schema.


Now coming to * <parent> <0x1> . and * * <0x1> . type of deletions. The way to do the * <parent> <0x1> deletion is what @MichelDiz has already pointed out, that is having a reverse index on the predicates and doing a delete upsert operation.

upsert {
  query {
     v as var(func: eq(name, "Bart")) {
      p as ~parent_to
    }
  }

  mutation {
    delete {
      # delete the entity
      uid(v) * * .
      # Also delete node corresponding to Bart from other parent_to edges.
      uid(p) <parent_to> uid(v) . 
    }
  }
}

What @campoy is suggesting is syntactic sugar around the same functionality which already exists, so I am not sure if we should be doing it now.

Regarding the * * <0x1> delete operation.

I’d argue we could also support this operation by finding the predicates in the same way we’ve done so far, but failing if any of those predicates doesn’t have a reverse index.

We would need to execute this operation for all predicates (or all predicates with a reverse index) in the database to support this as we don’t know what predicates might have a particular object (say 0x1). I’d say its better if the user gives us the predicates using an upsert delete operation.