apoc.ttl.expireIn

Procedure Apoc Extended

CALL apoc.ttl.expireIn(node,timeDelta,'time-unit') - expire node after specified length of time time by setting :TTL label and ttl property

Signature

apoc.ttl.expireIn(node :: NODE?, timeDelta :: INTEGER?, timeUnit :: STRING?) :: VOID

Input parameters

Name Type Default

node

NODE?

null

timeDelta

INTEGER?

null

timeUnit

STRING?

null

Enable TTL

By default TTL is disabled. We can enable it by setting the following property in apoc.conf:

apoc.conf
apoc.ttl.enabled=true

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
CREATE (Hugo:Person {name:'Hugo Weaving', born:1960})
CREATE (LillyW:Person {name:'Lilly Wachowski', born:1967})
CREATE (LanaW:Person {name:'Lana Wachowski', born:1965})
CREATE (JoelS:Person {name:'Joel Silver', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);

We can expire any people that have produced a movie by running the following query:

MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person)
CALL apoc.ttl.expireIn(person, 10,'s')
RETURN movie, produced, person;
Table 1. Results
movie produced person

(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: 1999})

[:PRODUCED]

(:Person:TTL {name: "Joel Silver", ttl: 1605698768575, born: 1952}

This node (and its relationships) will expire at 1605698768575 (10 seconds from now at the time of writing):

RETURN datetime({epochMillis: 1605698768575}) AS expiryTime;
Table 2. Results
expiryTime

2020-11-18T11:26:08.575Z

The next time that the expiration job runs, we’ll see the following output in neo4j.log:

neo4j.log
2020-11-18 11:26:40.357+0000 INFO  [apoc] TTL: Expired 1 nodes 1 relationships

And if we try to look for our producer:

MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person)
RETURN movie, produced, person;

We won’t get any results:

Table 3. Results
movie produced person