neo4j
  1. neo4j-delete-a-node

Delete a Node

In Neo4j, to delete a node, you can use the DELETE clause with the DETACH keyword. This command will remove the specified node and all its relationships from the graph.

Syntax

The syntax to delete a node in Neo4j is as follows:

MATCH (node)
WHERE <conditions>
DELETE <variables>

The MATCH clause is used to identify the node to be deleted, along with any other nodes or relationships that need to be identified. The WHERE keyword can be used to specify conditions that must be satisfied by the node being deleted. Finally, the DELETE clause is used to identify which variables should be deleted.

The DETACH keyword can be used along with DELETE to remove the node and all its relationships.

Example

Consider the following graph:

(p1:Person {name: 'John'})-[:FRIENDS]->(p2:Person {name: 'Jane'})
(p2)-[:WORKS_AT]->(c:Company {name: 'ABC Corp'})

To delete the node representing the person named "John" along with all its relationships, you can use the following query:

MATCH (p:Person {name: 'John'})
DETACH DELETE p

This query will delete the node with the name property set to "John" and all its relationships.

Output

After executing the above query, the node representing "John" and all its relationships will be deleted from the graph.

Explanation

In the example above, we used the MATCH clause to identify the node with name property "John". We then used the DETACH DELETE clause to remove the node and all its relationships from the graph.

Use

Deleting a node in Neo4j can be useful in situations where you want to remove a node along with its relationships, for example when cleaning up data or removing outdated information.

Important Points

  • DETACH DELETE deletes the node and all its relationships.
  • The MATCH clause is used to identify the node being deleted.
  • The WHERE keyword can be used to add conditions to the MATCH clause.

Summary

In Neo4j, deleting a node can be done using the DELETE clause with the DETACH keyword. This command will remove the specified node and all its relationships from the graph. The MATCH clause is used to identify the node being deleted, and the WHERE keyword can be used to specify conditions. The DETACH keyword is used along with DELETE to remove the node and all its relationships.

Published on: