5

I am learning neo4j , i want to know that is there any way that i can create a relationship or a node that will be delete automatically after a certain period of time.

Govind Singh
  • 15,282
  • 14
  • 72
  • 106

4 Answers4

4

As pointed out by @Scott in the comments, you can specify a TTL on nodes by using APOC as shown here. Append the following to your neo4j.conf:

apoc.ttl.enabled=true

Then you can either set the appropriate label and property yourself:

SET n:TTL
SET n.ttl = timestamp() + 3600

or utilize one of the following procedures:

// Expires in
CALL apoc.date.expire.in(node,time,'time-unit')

// Expires at
CALL apoc.date.expire(node,time,'time-unit')
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
3

There's nothing that I know of like this. Neo4j is just a database like *SQL or MongoDB (though let me know if they can do something like this).

The best suggestion that I would have is to put a delete_after property (or something similar) on the relationships and then have a job which queries on a regular basis to clean them up. Note that you can't query for relationships directly (that is, nodes always need to be involved in your query) so depending on how big your database is, you may need to think through what sort of index you need. I'm a bit vague here because I don't know what your domain model would look like.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • i am not sure about `mongodb` but `cassandra` have `ttl`. – Govind Singh Feb 13 '15 at 13:43
  • 2
    Neo4j doesn't have ttl but it could be easily added as an add-on. You would have to hook into a TxEvent Handler that keeps track of created nodes and rels with ttl properties. And then it would set timers for a deletion. – Michael Hunger Feb 14 '15 at 21:37
  • 2
    MongoDB has this feature. You can create indexes with expiration time. – Pikachu Aug 17 '15 at 18:19
  • 3
    version 3.0.x has TTL https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_timetolive_ttl_expire_nodes – Scott Dec 28 '16 at 03:09
2

If you are like me and stumble to this article, this has recently been updated. Ref: https://neo4j.com/labs/apoc/4.3/overview/apoc.ttl/apoc.ttl.expireIn/

    Match(person:person {id: 100})
    CALL apoc.ttl.expireIn(person, 10,'s')
    Return person;
Namrata
  • 59
  • 5
1

Another option for Neo4j is using a Neo4j extension by GraphAware: neo4j-expire

One disadvantage of using such extensions is sometimes they stop supporting them for newer versions of Neo4j and also it takes some time for them to support the latest version. If these things are not a problem with you, you should have no problem with the extension.

Himanshu Jain
  • 1,809
  • 1
  • 13
  • 23