Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove label reference from neo4j

Tags:

neo4j

While importing a ton of data from spreadsheets, I attempted to use a labeling convention where nodes were capitalized like "This" and labels for relationships were labeled like "THIS". In one case, I accidentally used the relationship label format for a set of nodes. I then deleted those nodes and reimported them with the correct label format. (Side question - Was there a way to rename a label that I didn't see which could have avoided the delete/reimport?)

My problem is that in the built-in Cypher browser (Neo4j 2.1.3), both the right and wrong labels show up on the node list, even though there are zero nodes with the wrong label. So while I successfully removed the nodes, I can't figure out how to remove the label - not from nodes, which is easy enough using the REMOVE command, but from the database entirely. Why didn't it remove this label automatically when the items it was assigned to reached zero?

To be more specific, I can click on the node label for MEASURES and this query fires:

MATCH (n:`MEASURES`) RETURN n LIMIT 25

with these results:

Returned 0 rows in 77 ms

I would like to completely remove the label 'MEASURES' from the database since nothing is using it. Please let me know if you need further info.

like image 699
Son of Arathorn Avatar asked Jan 30 '26 08:01

Son of Arathorn


2 Answers

I don't think there is yet a builtin way to remove no-longer-used labels entirely from a neo4j DB. I have also been annoyed by obsolete labels still showing up in places like the neo4j browser web UI.

I know of one way to remove them, but it may not be practical if your DB is enormous, and it might not be totally safe. Therefore, if you choose to do the following, you should make sure you have your original DB backed up (for example, you should make a copy of your original graph.db folder or rename it).

The technique is actually very simple. You just export all the data, shut down neo4j, delete or rename the original graph.db file, restart neo4j, and then re-import the data. The following steps assume that you are in your neo4j installation folder in a linux environment, and neo4j is not running as a service.

  1. Export the data (as CYPHER statements that will recreate the data):

    ./bin/neo4j-shell -c "dump" > mydump.cql

  2. Shut down neo4j (as it is not safe to remove or rename graph.db while the DB is running):

    ./bin/neo4j stop

  3. Rename the current graph.db folder, just in case you need to replace the new folder created below:

    mv data/graph.db data/graph.db.archive

  4. Restart neo4j, which will automatically create a new graph.db folder:

    ./bin/neo4j start

  5. Re-import the data from the dump:

    ./bin/neo4j-shell -file mydump.cql

The obsolete labels should be gone at this point from everywhere (you should reload any neo4j web pages).

like image 140
cybersam Avatar answered Feb 02 '26 01:02

cybersam


Here is how to do it.

What you have to make sure is that

  • 1) no node is using the label
  • 2) and there are no indices or constraints on the label

1) Removing/renaming the label on nodes with a cypher query:

MATCH (n:OldLabel)
SET    n:NewLabel /* Optional line if you want to rename the label */
REMOVE n:OldLabel
RETURN n

2a) Check if indices or constraints are using the label using the schema command in the neo4j-shell:

$ neo4j-shell 
Welcome to the Neo4j Shell! Enter 'help' for a list of commands
NOTE: Remote Neo4j graph database service 'shell' at port 1337

neo4j-sh (?)$ schema
Indexes
  ON :OldLabel(id)            ONLINE (for uniqueness constraint) 
  ON :Person(name)            ONLINE (for uniqueness constraint) 
  ON :Person(id)              ONLINE (for uniqueness constraint) 

Constraints
  ON (person:Person) ASSERT person.name IS UNIQUE
  ON (person:Person) ASSERT person.id IS UNIQUE
  ON (oldlabel:OldLabel) ASSERT oldlabel.id IS UNIQUE

2b) Remove index and constraint in a cypher query:

DROP CONSTRAINT ON (n:OldLabel)
   ASSERT n.id IS UNIQUE;
DROP INDEX ON :OldLabel(id);

Remember to make new indices and constraints if you just wanted to rename the label.

After this the label should no longer show up in the web interface.

like image 28
Louis Avatar answered Feb 02 '26 00:02

Louis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!