PERSIST
Removes the expiration from a key, making it persist indefinitely.
Syntax
PERSIST keyDescription
The PERSIST command removes the expiration from a key, making it persist indefinitely (until explicitly deleted). If the key doesn't exist or has no expiration set, the command returns 0.
Arguments
key(required): The name of the key to remove expiration from.
Return Value
Returns an Integer:
1if the expiration was removed successfully0if the key doesn't exist or has no expiration set
Examples
Removing Expiration
SET mykey "value"
EXPIRE mykey 3600
PERSIST mykey
TTL mykeyResponse:
OK
(integer) 1
(integer) 1
(integer) -1 # No expirationKey Without Expiration
SET mykey "value"
PERSIST mykeyResponse:
OK
(integer) 0Non-Existent Key
PERSIST nonexistentResponse:
(integer) 0Making Expired Key Persistent
SET mykey "value"
EXPIRE mykey 1
# Wait 1 second - key has expired
PERSIST mykeyResponse:
OK
(integer) 1
(integer) 0 # Key doesn't exist anymoreNotes
- Removes the expiration, making the key persist indefinitely
- Returns
0if the key doesn't exist - Returns
0if the key has no expiration set - Works with both string and list keys
- Once persisted, the key will not expire unless a new expiration is set
Use Cases
- Making Temporary Data Permanent: Convert temporary keys to permanent ones
- Configuration Changes: Remove expiration from configuration keys
- Data Preservation: Ensure important data doesn't expire
- Dynamic Expiration Management: Toggle expiration on and off as needed