Neo4j CQL - REMOVE



Sometimes based on our client requirements, we need to add or remove properties to/from existing Nodes or Relationships.

We use Neo4j CQL SET clause to add new properties to existing Nodes or Relationships.

We use Neo4j CQL REMOVE clause to remove existing properties of Nodes or Relationships.

Neo4j CQL REMOVE command is used

  • To remove labels of a Node or a Relationship
  • To remove properties of a Node or a Relationship

Main Difference between Neo4j CQL DELETE and REMOVE commands -

  • DELETE operation is used to delete Nodes and associated Relationships.
  • REMOVE operation is used to remove labels and properties.

Similarity between Neo4j CQL DELETE and REMOVE commands -

  • Both commands should not be used as alone.
  • Both commands should be used with MATCH Command.

Remove Property of a Node/Relationship

We can use same syntax to remove a property or list of properties of a Node or a Relationship permanently from the Database.

REMOVE Property clause syntax

REMOVE <property-name-list>
S.No.Syntax ElementDescription
1.REMOVEIt is a Neo4j CQL keyword.
2.<property-name-list>It is a list of properties to remove it from a Node or a Relationship permanently.

<property-name-list> syntax

<node-name>.<property1-name>,
<node-name>.<property2-name>, 
.... 
<node-name>.<propertyn-name> 

Syntax Description

S.No.Syntax ElementDescription
1.<node-name>It is a name of a Node.
2.<property-name>It is a property name of a Node.

NOTE -

  • We should use comma(,) operator to separate the label names list.
  • We should use dot(.) operator to separate the node names and label name.

Example

This example demonstrates how to Create a Node and to delete a Property from this Node permanently from the Database.

Step 1 - Open Neo4j Data Browser

Neo4j CQL Tutorial

Step 2 - Type the below command on Data Browser

CREATE (book:Book {id:122,title:"Neo4j Tutorial",pages:340,price:250}) 
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

It is similar to the below two SQL Commands at a single shot.

CREATE TABLE BOOK(
	id number,
	title varchar2(20),
	pages number,
	pages number
);
INSERT INTO BOOK VALUES (122,'Neo4j Tutorial',340,250);

Here we can observe that one label and one node with 4 properties are created successfully.

Step 4 - Type the below command on Data Browser

MATCH (book : Book)
RETURN book

It is similar to the below SQL Command.

SELECT * FROM BOOK;

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

Neo4j CQL Tutorial

Here we can observe that this book node has 4 properties.

Step 6 - Type the below command on Data Browser and click on Execute button to remove "price" property from book node.

MATCH (book { id:122 })
REMOVE book.price
RETURN book

It is similar to the below SQL Command.

ALTER TABLE BOOK REMOVE COLUMN PRICE;
SELECT * FROM BOOK WHERE ID = 122;
Neo4j CQL Tutorial

Here we can see only 3 properties of node book because "price" property is deleted.

Sometimes based on client requirements, we need to remove some existing properties to Nodes or Relationships.

We need to use REMOVE clause to remove a property or set of properties.

Example

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

Step 1 - Open Neo4j Data Browser

Step 2 - Type the below command on Data Browser

MATCH (dc:DebitCard) 
RETURN dc
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

Here we can observe that DebitCard node contains 6 properties.

Step 4 - Type the below command on Data Browser

MATCH (dc:DebitCard) 
REMOVE dc.cvv
RETURN dc
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

If we observe the output, "cvv" property is removed from "DebitCard" node..

To Remove a Label of a Node/Relationship

We can use same syntax to remove a Label or list of Labels of a Node or a Relationship permanently from the Database.

REMOVE a Label clause syntax:

REMOVE <label-name-list>
S.No.Syntax ElementDescription
1.REMOVEIt is a Neo4j CQL keyword.
2.<label-name-list>It is a list of labels to remove it from a Node or a Relationship permanently.

<label-name-list> syntax

<node-name>:<label1-name>,
<node-name>:<label2-name>, 
.... 
<node-name>:<labeln-name> 

Syntax Description

S.No.Syntax ElementDescription
1.<node-name>It is a name of a Node.
2.<label-name>It is a label name of a Node.

NOTE -

  • We should use comma(,) operator to separate the label names list.

  • We should use colon(:) operator to separate the node names and label name.

Example

This example demonstrates how to remove an unwanted label to a Node permanently from the Database.

Step 1 - Open Neo4j Data Browser

Step 2 - Type the below command on Data Browser

MATCH (m:Movie) RETURN m
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

Step 4 - Click on node to see its properties window.

Neo4j CQL Tutorial

Here we can observe that four labels are available to a single node.

As per our client requirement, we need to remove "Picture" label to this node

Step 5 - Type below command on Browser and click on Execute button.

MATCH (m:Movie) 
REMOVE m:Picture
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

Here we can observe that one label is removed from node permanently from the Database.

Step 7 - Type the below command on Data Browser

MATCH (m:Movie) RETURN m
Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

Step 9 - Click on node to see its properties window.

Neo4j CQL Tutorial

Here we can observe that this node has only three labels :Movie,Cinema,Film from Properties Window. That means our previous command has removed Picture label successfully.

Advertisements