Why can't I delete all labels and properties in Neo4j? - neo4j

I used "Match (n) detach delete n" to try delete everything in the graph, but in Neo4j Browser it still shows there is one label and a few properties. That looks like strange.
Please see my attached screenshot. Thanks.

If you have indexes present, the labels and properties used in the indexes will show in the browser. Try looking at :schema.
Also keep note that this is meta data, it isn't necessarily the most accurate reflection of what's actually in your database.

Related

Remove property from all nodes in a Neo4j database

I've inherited a Neo4j database which is massive to say the least. I've been looking into it and noticed we store a large string in each node that is not used.
Let's say we have a few million nodes called Post and each Post has Text. I'd like to remove Text from each Post in the entire database. Can someone point me down the path of doing so via Cypher?
Also, how would you recommend safely doing this? Should I back up the entire instance incase something goes wrong? Or does Neo4j have a transactional history I can rely on?
Neo4j does not allow NULL in the property so you can REMOVE Text from Post. Here is the syntax:
MATCH (p:Post)
REMOVE p.text
However, you said you have a massive database so I would recommend to remove it by batch.
CALL apoc.periodic.iterate(
"MATCH (p:Post) RETURN p",
"REMOVE p.text",
{batchSize:10000, parallel:true})
It would create batches of 10k Post nodes then remove the property text then run it in parallel.
In any mass updates that you do, it is highly recommended to create a backup of the database.
This code should delete all of the p.text in the nodes that hold it:
MATCH (p:Post)
WHERE exists(p.text)
REMOVE p.text

How to automatically expand child relationships in neo4j

When I run a query I can see the nodes correctly. But I need to go one by one and click "Expand child relationships" which is tedious and time consuming. Is there any way to see the graph with everything already expanded?
Thanks!
You're talking about Neo4j Browser, right?
If so, you need to specify exactly what you need to visualize first. So let's say you have a User node that is connected to a Book node with a read relationship.
Instead of just
MATCH (u:User)
RETURN u
And expanding that node to see all other connected nodes, just do
MATCH (u:User)-[:read]->(b:Book)
RETURN *
And just add the relationships you want in the query itself.
You can, however, do
MATCH (n) RETURN n
Which will return every node with its relationship, but there's a limit to how many nodes you can see. You can extend the limit in the settings (bottom left of the navigation bar) and tinkering with Graph Visualization values. This is not recommended and, depending on the size of your graphdb, it could cause a bottleneck and it can take a lot of time.
My advice, just write a query that shows exactly which nodes and relationships you want to see.
The Neo4j Browser supports an "auto-completion" mode that, when enabled, automatically queries for (and displays) the relationships between the nodes returned by a Cypher query.
In the most recent versions of the Browser, you can enable auto-completion mode by clicking the gear icon on the left side, scrolling to the bottom of the Browser Settings panel, and check-marking the "Connect result nodes" option.

remove all label with a Cypher query?

I deleted all of node and relationship. Now, I want to delete all existing labels with a Cypher query but I can't.
You are probably referring to the neo4j browser's "Node labels" display. The browser can continue to display labels that have been deleted from all nodes (or even if the DB no longer has any nodes). This is really just a minor nuisance.
As long as your Cypher queries show that there are no nodes with that label, rest assured that the label does not "really" exist in the DB.
If you're removing all of the data (nodes and relationships) anyway, you might as well delete your graph.db directory or wherever you store your data. This will also result in having pre-existing labels not show up in the browser.
This will also remove all indexes you might have had set up.

Neo4j - is it possible to visualise a simple overview of my database?

I've got my graph database, populated with nodes, relationships, properties etc. I'd like to see an overview of how the whole database is connected, each relationship to each node, properties of a node etc.
I don't mean view each individual node, but rather something like an ERD from a relational database, something like this, with the node labels. Is this possible?
You can use the metadata by running the command call db.schema().
In Neo4j v4 call db.schema() is deprecated, you can now use call db.schema.visualization()
As far as I know, there is no straight-forward way to get a nicely pictured diagram of a neo4j database structure.
There is a pre-defined query in the neo4j browser which finds all node types and their relationships. However, it traverses the complete graph and may fail due to memory errors if you have to much data.
Also, there is neoprofiler. It's a tool which claims to so what you ask. I never tried and it didn't get too many updates lately. Still worth a try: https://github.com/moxious/neoprofiler
Even though this is not a graphical representation, this query will give you an idea on what type of nodes are connected to other nodes with what type of relationship.
MATCH (n)
OPTIONAL MATCH (n)-[r]->(x)
WITH DISTINCT {l1: labels(n), r: type(r), l2: labels(x)}
AS `first degree connection`
RETURN `first degree connection`;
You could use this query to then unwind the labels to write that next cypher query dynamically (via a scripting language and using the REST API) and then paste that query back into the neo4j browser to get an example set of the data.
But this should be good enough to get an overview of your graph. Expand from here.

How to delete labels in neo4j?

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?
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
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
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 .
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
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)
If you delete the index of that labels, then it will delete the labels from database.
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.
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

Resources