How to delete labels in neo4j?
Asked Answered
S

9

27

How to delete labels in neo4j? Actually I deleted all nodes and relationships, then I recreated the movie database and still the labels I created before appeared on the webinterface. I also tried to use a different location for the database and even after an uninstall and reinstall the labels still appeared. Why? Where are the labels stored? After the uninstall the programm, the database folder and the appdata folder were deleted.

How to reproduce? Install neo4j -> use the movie database example -> create (l:SomeLabel {name:"A freaky label"}) -> delete the node -> stop neo, create new folder -> start neo -> create movie shema -> match (n) return (n) -> SomeLabel appears, even if you changed the folder or make an uninstall / install.

Is there a way to delete labels even if there is no node with it?

Stinkstone answered 24/2, 2014 at 9:18 Comment(0)
S
16

There isn't at the moment (Neo4j 2.0.1) a way to explicitly delete a label once it has been created. Neo4j Browser will display all labels which are reported by the REST endpoint at:

http://localhost:7474/db/data/labels

Separately, the Neo4j Browser sidebar which displays labels doesn't properly refresh the listing when it loses connection with Neo4j. A web browser reload should work.

Lastly, there was a bug in Neo4j Browser's visualization which would display all labels for which a style had been created. If using a version of Neo4j which has the bug, you can clear the styling by clicking on "View Stylesheet" in the property inspector, then clicking the fire extinguisher icon. All of that needs usability improvement, admittedly.

Cheers, Andreas

Sazerac answered 24/2, 2014 at 18:16 Comment(5)
Good to know. REST returns only the labels from the movie database, and even in the neo4j browser sidebar onlye the movie database labels appear. But what I really meant by the question is the neo4j broser visualization: When I ask for MATCH (n) RETURN n then all the old labels appear even after uninstall/reinstall/browser refresh. That is really weird.Stinkstone
Updated my response to include mention of a bug in the browser visualization. There are some outstanding UI/UX quirks which will get fixed.Sazerac
In Neo4j 3.0.1, all I needed to do is remove the label from all nodes, and then remove any index on the label. As soon as I removed the index, the label was gone from the sidebar. DROP INDEX ON :Label(property)Spallation
@Spallation can you share a bigger example on this because I cannot seem to do this in 4.4* browser version.Saddlecloth
@Saddlecloth sorry, this is an old comment. I don't have the resources to do so.Spallation
H
14

This seems to be worked out by version 2.3.0.

As an example, suppose we had created a movie in the data browser such as:

CREATE(m:Movie:Cinema:Film:Picture{title:"The Matrix"})

We could query it with

MATCH(m:Movie)
WHERE m.title = "The Matrix"
RETURN m

It would have 4 labels: Movie, Cinema, Film, and Picture

To remove the Picture label from all movies:

MATCH(m:Movie)
REMOVE m:Picture
RETURN m

To remove the Picture label from only that one movie:

MATCH(m:Movie)
WHERE m.title = "The Matrix"
REMOVE m:Picture
RETURN m
Hahnke answered 15/11, 2015 at 17:43 Comment(2)
How to remove all labels in a single shot?Toboggan
@JackDaniel I haven't used Neo4j in a while. It's probably best you open your question as a new question here on StackOverflow. You can link to this answer as an effort to show your research so far.Hahnke
B
4

Let us assume that we have created a node Product as below

PRODUCT_MASTER { product_code :"ABC", product_name:"XYX }

CREATE INDEX ON :PRODUCT_MASTER (product_code);

Now even if I delete all PRODUCT_MASTER nodes from graph, we will keep getting PRODUCT_MASTER in browser under Node labels. To get rid of the same , we need to drop the index as well.

DROP INDEX ON :PRODUCT_MASTER (product_code);

In neo4j-shell , type in "schema" command to get the list of indexes and corresponding properties.

To summarize , in case we delete all of the nodes of particular type , you need delete indexes on that node as well .

Beeves answered 2/2, 2017 at 5:35 Comment(0)
H
3

I simply:

  • stop neo4j
  • delete the entire database, and that removes everything
  • start neo4j

on a mac the db is here /usr/local/var/neo4j/data/databases/graph.db

Holocrine answered 17/12, 2019 at 9:43 Comment(0)
R
1

The reason is that when a label is created, Neo4j indexes this label. You can delete the node but the index will remain.

At a guess - if you drop the index on the label, it will disappear from the GUI (NOTE- I've not got access to Neo4j at the moment to check this theory)

Ruthful answered 25/2, 2014 at 8:51 Comment(3)
No, Neo4j will internally keep labels around forever.Messina
@MichaelHunger, will it be possible in the future releases to delete labels from the database completely?Riesling
Dropping the indexes and constraints for the label resulted in it disappearing from the web interface. Thanks!Pedo
Q
1

If you delete the index of that labels, then it will delete the labels from database.

Quinacrine answered 9/7, 2020 at 5:16 Comment(0)
J
1

This will work on neo4j 5.x:

Make sure you remove all Constraints and Indexes after removing the entities;

Get the Schema to understand which Constraints and Indexes are left using:

:schema

Go over each one of the Constraints and remove them using (note that the indexes might be connected to the Constraints thus will be also removed):

DROP CONSTRAINT imp_uniq_my_constraint

If needed, drop the indexes too:

DROP INDEX index_f7700477

Check your Schema to check what's left > the labels will be removed accordingly.

Jecoa answered 17/12, 2023 at 16:10 Comment(3)
They should do this in the Clean up section of the Movie Graph Guide.Surrogate
Do you know how to clean up those Property keys?Surrogate
looks like the team don't give a shit. github.com/neo4j/neo4j/issues/10941Surrogate
M
0

I just found a workaround (with neo4j 2.2 M04). Dump the content of the DB to a file, throw away the DB, then insert the dump again. Only works for small DBs, though.

Step1: dump the content, using neo4j-shell

$NEO4J_HOME/bin/> neo4j-shell -c 'dump match a return a;' > dump.temp

Step2: throw away DB (there's plenty ways to delete the folder $NEO4J_HOME/data/graph.db/ or wherever your DB folder is)

Step3: insert the dump again, using neo4j-shell

$NEO4J_HOME/bin/> neo4j-shell -file dump.temp

This should bring up statistics on how many nodes, relationships, properties and labels have been created.

(And Step4 would be to delete that dump.temp file, it has no reason to live inside the bin folder.)

What I find odd (and maybe Michael or somebody else from neo4j could shed some light on this): in my case, Step3 told me that some 50+ labels had been created. However, when I open the web interface, only those 15 or so labels, which I actually use, are listed. So the DB feels clean now. Not entirely sure that it is clean.

Mobley answered 4/3, 2015 at 11:43 Comment(0)
I
0

As of today, with Neo4j Desktop Version: 1.1.10 and DB Version: 3.4.7 Delete data + delete Index + delete any unique constraints + Developer > Refresh clears all Labels

Incunabula answered 31/10, 2018 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.