Delete non-existing nodes and relationships

I'm currently writing unit tests that handle the case of deletion of non-existing nodes and relationships by their ID. Both queries should return the deleted nodes and relationships as array.

Delete nodes by ID:

  MATCH (n)
  WHERE id(n) IN $ids
  OPTIONAL MATCH (n)-[r]-()
  DETACH DELETE n
  RETURN COLLECT(DISTINCT n) as nodes, COLLECT(DISTINCT r) as relationships

Delete relationships by ID:

  MATCH ()-[r]->()
  WHERE id(r) IN $ids
  DELETE r
  RETURN [] as nodes, COLLECT(DISTINCT r) as relationships

If I test both queries with IDs $ids = [-1, -2] then I get different results even though the IDs don't exist in both cases.

The node query returns one record with two empty arrays:

[
  {
    "keys": [
      "nodes",
      "relationships"
    ],
    "length": 2,
    "_fields": [
      [],
      []
    ],
    "_fieldLookup": {
      "nodes": 0,
      "relationships": 1
    }
  }
]

Whereas the relationships query returns no record at all:

[]

Why is this the case?

Sorry I can not find the reference, but I recall reading that negative id's are possible. They are used internally to track something. The node that was returned with a negative id does not even represent a node entity, as the properties are not the same as a node entity. Maybe relationships with negative id's don't have any properties to return, thus your result.

Maybe try $ids = [null] instead of negative numbers to test for non-existing nodes and relationships. You will get ',' as a result for the node query and no result for the relationship query. The reason for the result for the node query is that you have only 'collect' methods, which will return empty lists when there are not result records. In the case of the relationship record, you have ' as nodes', so nothing is returned when there are no result records.