Neo4j CQL - DELETE



Neo4j CQL DELETE clause is used

  • To delete a Node.
  • To delete a Node and associated Nodes and Relationships.

We are to going to discuss how to delete a node in this chapter. We will discuss how to delete a Node and associated Nodes and Relationships in next chapter.

To Delete a Node -

By using this command, we can delete a Node and its associated properties permanently from the Database.

DELETE a Node clause syntax

DELETE <node-name-list>
S.No. Syntax Element Description
1. DELETE It is a Neo4j CQL keyword.
2. <node-name-list> It is a list of node names, which are going to delete from Database.

NOTE -

We should use comma(,) operator to separate the node names.

Example

This example demonstrates how to delete a Node permanently from the Database.

Step 1 - Open Neo4j Data Browser.

Step 2 - Type the below command on Data Browser

MATCH (e: 'Employee') RETURN e 

NOTE -

MATCH (e: 'Employee') RETURN e

MATCH (e: "Employee") RETURN e

MATCH (e: Employee) RETURN e

All three commands are same, we can choose any one of these commands.

Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

Here we can observe that there is one node available with "Employee" name in the Database.

Step 4 - Type the below command on Data Browser.

MATCH (e: Employee) DELETE e

Now instead of "RETURN e", use "DELETE e" command to delete Employee node

Neo4j CQL Tutorial

Step 5 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

Here we can observe that one node is deleted from the Database.

Now check whether the Employee node is deleted from Database or not.

Step 6 - Type the below command and click on Execute command.

MATCH (e: Employee) RETURN e
Neo4j CQL Tutorial

Here we can observe that Employee node is deleted permanently as zero rows returned by this query.

DELETE Node and Relationship clause syntax

DELETE <node1-name>,<node2-name>,<relationship-name>
S.No. Syntax Element Description
1. DELETE It is a Neo4j CQL keyword.
2. <node1-name> It is one end node name used to create a relationship <relationship-name>.
3. <node2-name> It is another node name used to create a relationship <relationship-name>.
4. <relationship-name> It is a relationship name, which is created between <node1-name> and <node2-name>.

NOTE -

We should use comma(,) operator to separate the node names and relationship name.

Example

This example demonstrates how to delete a Node and its associate Node and relationship permanently from the Database.

Step 1 - Open Neo4j Data Browser

Step 2 - Type the below command on Data Browser

MATCH (cc:CreditCard)-[r]-(c:Customer)RETURN r 
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

Here we are observing that one node for Customer, one node for CreditCard and a relationship between them is available.

Step 4 - Type the below command on Data Browser

MATCH (cc: CreditCard)-[rel]-(c:Customer) 
DELETE cc,c,rel
Neo4j CQL Tutorial

Step 5 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

Here we can observe that two nodes and their associated 10 relationships are deleted successfully.

Now check whether the DELETE operation is done successfully or not.

Step 6 - Type the below command on Data Browser.

MATCH (cc:CreditCard)-[r]-(c:Customer) RETURN r
Neo4j CQL Tutorial

Step 7 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

Here we can observe that zero rows returned from the database.

Advertisements