apoc.ttl.expire

Procedure APOC Full

CALL apoc.ttl.expire(node,time,'time-unit') - expire node at specified time by setting :TTL label and ttl property

Signature

apoc.ttl.expire(node :: NODE?, time :: INTEGER?, timeUnit :: STRING?) :: VOID

Input parameters

Name Type Default

node

NODE?

null

time

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:

WITH apoc.date.add(datetime().epochSeconds, "s", 5, "s") AS fiveSecondsTime
MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person)
CALL apoc.ttl.expire(person,fiveSecondsTime,'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: 1605698172000, born: 1952})

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

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

2020-11-18T11:16:12Z

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

neo4j.log
2020-11-18 11:16:40.348+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