2

https://dgraph.io/tour/schema/8/

shows some options for deleting

  1. Delete a single triple
  2. Delete all triples for a given edge
  3. Delete all triples for a given node

Now i'd like to delete all triple for nodes of a given type. I assume this is done by some kind of combination of a query that selects the nodes for the the given type and then a mutation for each of these nodes. I couldn't find an example for this in the tutorial.

Let's assume I'd like to delete all triples for nodes of the type Country.

I know how to select the uids for the nodes:

    {  
      query(func: has(<dgraph.type>)) @filter(eq(<dgraph.type>, "Country")) {
        uid
      }
    }

But how do i combine this with a mutation?

https://discuss.dgraph.io/t/how-to-bulk-delete-nodes-or-cascade-delete-nodes/7308

seems to ask for an "upsert"

How could the deletion of all triples for nodes with a given type be achieved?

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

1 Answers1

4

the following upsert seems to work:

upsert {  
  query {
    # get the uids of all Country nodes
     countries as var (func: has(<dgraph.type>)) @filter(eq(<dgraph.type>, "Country")) {
        uid
    }
  }
  mutation {
    delete {
      uid(countries) * * .
    }
  }
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • That doesn't scales well. Has func is a too wide func. You should use eq instead. If you use has, it will do a lookup for all types on your system and then apply the filter. If you use the eq already in the root, Dgraph would look right away in the index table and execute the operation. – Michel Conrado Diz Aug 26 '20 at 02:47
  • @MichelConradoDiz thx for the hint. what would the query look like then? – Wolfgang Fahl Aug 26 '20 at 05:55
  • 1
    Just like `countries as var(func: eq(, "Country") )` or `countries as var(func: type("Country") )` – Michel Conrado Diz Aug 27 '20 at 18:11