Yehia Awad
Yehia Awad

Reputation: 2978

REDIS how to remove Expiry from a key

I have searched fair enough to conclude that such question was not asked yet.

Let's Suppose we have a REDIS SET KEY:

SET TEST_KEY 1
TTL TEST_KEY ## -1 (unlimited)
EXPIRE TEST_KEY 1000 # 1
TTL TEST_KEY ## 1000 (1000 seconds)

Let's say now I wish to remove the expiry from this specific key so's TTL would return -1, what to do ? I have tried setting the EXPIRE to -1 but it didn't work

Upvotes: 6

Views: 9330

Answers (2)

flynn
flynn

Reputation: 381

Use PERSIST commmand

redis> SET mykey "Hello"
"OK"
redis> EXPIRE mykey 10
(integer) 1
redis> TTL mykey
(integer) 10
redis> PERSIST mykey
(integer) 1
redis> TTL mykey
(integer) -1
redis> 

Upvotes: 9

for_stack
for_stack

Reputation: 22906

Just reset it with the SET command:

SET TEST_KEY 1
TTL TEST_KEY  ## -1

A more general command, which also works with LIST, SET and so on, is PERSIST

PERSIST TEST_KEY
TTL TEST_KEY   ## -1

Upvotes: 10

Related Questions