Reputation: 49
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.
Upvotes: 3
Views: 2608
Reputation: 832
Here is how to do it.
What you have to make sure is that
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.
Upvotes: 5
Reputation: 67044
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.
Export the data (as CYPHER statements that will recreate the data):
./bin/neo4j-shell -c "dump" > mydump.cql
Shut down neo4j (as it is not safe to remove or rename graph.db
while the DB is running):
./bin/neo4j stop
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
Restart neo4j, which will automatically create a new graph.db
folder:
./bin/neo4j start
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).
Upvotes: 6
Reputation: 18022
Labels don't really exist apart from nodes that use them. You can always query for non-existant labels, and you'll always get zero nodes back.
Here, you're querying for MEASURES and getting nothing. That's pretty much the same thing as the label not existing.
Here's an example with a database I made just now:
$ neo4j-shell -path test
NOTE: Local Neo4j graph database service at 'test'
Welcome to the Neo4j Shell! Enter 'help' for a list of commands
neo4j-sh (?)$ MATCH (m:TotallyNonExistantLabel) return m;
+---+
| m |
+---+
+---+
0 row
1946 ms
So, the bottom line is that you can't really delete labels from your database other than deleting all of the nodes that use them. You can do that like this:
MATCH (f:ThisLabelGonnaDieSucka)
REMOVE f:ThisLabelGonnaDieSucka
RETURN f;
That's basically deleting ThisLabelGonnaDieSucka
from the database.
Upvotes: 1