How to Delete Unused Property Keys

I ran the following query to get all the property keys currently in use in my Neo4j database:

MATCH (n)
UNWIND keys(n) AS key
WITH DISTINCT key
RETURN key
ORDER BY key

This query returned all the property keys that are currently being used by nodes in my database. However, I know there is one property key in my database schema that is not being used by any node (I believe it is unused because it does not appear in the query results).

I want to delete this unused property key from the database, but I am unable to do so. Is there a way to remove unused property keys from Neo4j?

Any help would be really Appreciated!

Since Neo4j is schemaless, a property doesn't exist if it isn't used, and so there is nothing to delete.

An optional schema is being developed, but isn't released yet. However, it is possible to set some constraints that would partly enforce some kind of schema, is that what you mean with that that the property is in the schema? Or do you mean that you have an index on the property? If so, you can drop it with

DROP INDEX index_name

But i deleted all the nodes and relationships. However, the Property Node still there. And as you said using Index you can delete but i can not create index of the property key that is not attached to any node or relationship.

CREATE INDEX unusedPropertyIndex FOR (n:Label) ON (n.unusedProperty)

This query will not work because there are no nodes with the label Label or the property unusedProperty . I understand that indexes are tied to nodes or relationships, but is there a way to create an index for a property that is not currently in use?

@christoffer.bergman

You can create an index for labels and properties that doesn't exist. It is possible to create the index before you start populating the graph, and at that time the labels and properties doesn't exist yet. So this command works even if the graph is empty:

CREATE INDEX unusedPropertyIndex FOR (n:Label) ON (n.unusedProperty)

You wrote that you deleted all nodes and relationships, but that the "Property Node" is still there? What do you mean with "Property Node". Any node can have properties, so if all nodes are deleted you should also not have any properties anymore...

Can you give some example code of what you mean?

First of all, I want to apologize for the confusion in my earlier question where I mistakenly referred to property keys as property nodes . I noticed that even after deleting all nodes and relationships, some property keys still remain in the database schema. For example, when I run the following query:

CALL db.propertyKeys()

I see a list of all property keys, including one called name, which is no longer in use because I have deleted all nodes and relationships that used it. However, the name property key still appears in the schema metadata.

I want to delete this name property key which is unused.

@christoffer.bergman

Ah, sorry, I didn't realise you referred to the db.propertyKeys() function when you said the property keys were still in the database.

No, that function returns the name of any property that has ever been created, even if all instances of it has been deleted. The reason is that if you delete a node or remove a property, the database doesn't know if that was the last instance of it (without scanning the entire database, which could be very time consuming). It is unfortunately not possible to remove entries from what it returns.

But that doesn't mean the properties still exist. If the nodes are deleted, then the properties are gone (even if returned from db.propertyKeys()).

The only solution I can think of for getting the properties actually in use is listing properties with the code snippet you had in the original post, instead of using CALL db.propertyKeys(). I.e. this bit:

MATCH (n)
UNWIND keys(n) AS key
WITH DISTINCT key
RETURN key 
ORDER BY key

It takes more time to run (at least on a big database), but that is the time db.propertyKeys() would have taken if it would have returned the actually used properties.

So in short, I can not directly Delete unused Property Keys which is still visible in the browser?

I don't think so. Not easily at least.

There may be a way, I am not sure and don't have the possibility to test it, but that would be to backup the database using the copy command with the

using the --compact-node-store parameter
and then restoring the database again. It might be that it has cleared up old property keys after that. But again, I am not completely sure.

1 Like