Neo4j 简明教程

Neo4j - Delete Clause

你可以使用 DELETE 子句从数据库删除节点和关系。

You can delete nodes and relationships from a database using the DELETE clause.

Deleting All Nodes and Relationships

以下是使用 DELETE 子句从数据库删除所有节点和关系的查询:

Following is the query to delete all the nodes and the relationships in the database using the DELETE clause.

Query

MATCH (n) DETACH DELETE n

执行上述查询,执行以下步骤:

To execute the above query, carry out the following steps −

Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

detach match

这将删除 neo4j 数据库中的所有节点和关系并使其变空。

This will delete all the nodes and relationships from your neo4j database and make it empty.

Deleting a Particular Node

要删除具体节点,您需要在以上查询中将其详细信息指定在“n”的位置。

To delete a particular node, you need to specify the details of the node in the place of “n” in the above query.

Syntax

以下是使用 DELETE 子句从 Neo4j 中删除具体节点的语法。

Following is the syntax to delete a particular node from Neo4j using the DELETE clause.

MATCH (node:label {properties . . . . . . . . . .  })
DETACH DELETE node

Example

在继续举例之前,请按照以下指示在 Neo4j 数据库中创建一个名为“Ishant”的节点。

Before proceeding with the example, create a node “Ishant” in the Neo4j database as shown below.

CREATE (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})

以下是一个使用 DELETE 子句删除上述创建节点的示例 Cypher 查询。

Following is a sample Cypher Query which deletes the above created node using the DELETE clause.

MATCH (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
DETACH DELETE Ishant

执行上述查询,执行以下步骤:

To execute the above query, carry out the following steps −

Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

particular node

Result

执行后,您将获得以下结果。此处您可以观察到已删除指定节点。

On executing, you will get the following result. Here you can observe that the specified node is deleted.

deleted node