Skip to content

DELETE clause

Marijn van Wezel edited this page Dec 13, 2022 · 7 revisions

The DELETE clause is used to delete nodes, relationships or paths. It accepts a list of structural values to be deleted.

Query::delete(Pattern|StructuralType|(Pattern|StructuralType)[] $structures, bool $detach = false): Query
Query::detachDelete(Pattern|StructuralType|(Pattern|StructuralType)[] $structures): Query

Parameters

  • $structures : The structures to delete. If a Pattern is given, it is automatically converted to a variable.
  • $detach : Whether to DETACH DELETE.

Relevant methods

  • setDetach(bool $detach = true): self : Sets the clause to DETACH DELETE.
  • addStructure(Pattern|StructuralType ...$structures): self : Add one or more structures to delete.

Examples

Delete a single node

$unknown = node('Person')->withProperties([
    'name' => 'UNKNOWN',
]);

$query = query()
    ->match($unknown)
    ->delete($unknown)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Person {name: 'UNKNOWN'}) DELETE %s", $query);

Delete all nodes

$everything = node();

$query = query()
    ->match($everything)
    ->delete($everything)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s) DELETE %s", $query);

Delete all nodes and relationships

$everything = node();

$query = query()
    ->match($everything)
    ->delete($everything, true)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s) DETACH DELETE %s", $query);
$everything = node();

$query = query()
    ->match($everything)
    ->detachDelete($everything)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s) DETACH DELETE %s", $query);

Delete multiple (independent) nodes

$persons = node('Person');
$animals = node('Animal');

$query = query()
    ->match([$persons, $animals])
    ->delete([$persons, $animals])
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Person), (%s:Animal) DELETE %s, %s", $query);

External links