0
0
Redisquery~5 mins

PERSIST to remove expiry in Redis

Choose your learning style9 modes available
Introduction
Sometimes you want to keep a key forever in Redis without it disappearing after a set time. PERSIST helps by removing any expiration time from a key.
You set a temporary key but then decide to keep it permanently.
You want to stop a countdown timer on a key so it doesn't expire.
You accidentally set an expiry and want to undo it.
You want to make sure important data stays in Redis without disappearing.
Syntax
Redis
PERSIST key
This command removes the expiration from the given key.
If the key does not have an expiration, PERSIST does nothing and returns 0.
Examples
Removes the expiration from 'mykey' so it stays forever.
Redis
PERSIST mykey
Stops the expiration timer on 'session123' key.
Redis
PERSIST session123
Sample Program
We create a key 'tempkey' with value 'hello'. Then we set it to expire in 10 seconds. TTL shows the time left. PERSIST removes the expiry. TTL after PERSIST returns -1, meaning no expiry.
Redis
SET tempkey "hello"
EXPIRE tempkey 10
TTL tempkey
PERSIST tempkey
TTL tempkey
OutputSuccess
Important Notes
TTL command returns -1 if the key has no expiration.
PERSIST returns 1 if expiration was removed, 0 if key had no expiration or does not exist.
Removing expiry means the key will stay until deleted manually.
Summary
PERSIST removes the expiration time from a Redis key.
Use it to keep keys permanently after setting expiry.
Check TTL before and after to see the effect.