Skip to content

PERSIST

The PERSIST command is used to remove the expiration from a key in DiceDB. If a key is set to expire after a certain period, using the PERSIST command will make the key persistent, meaning it will no longer have an expiration time and will remain in the database until explicitly deleted.

Syntax

Terminal window
PERSIST key

Parameters

ParameterDescriptionTypeRequired
keyThe name of the key to persist.StringYes

Return Value

ConditionReturn Value
The timeout was successfully removed1
The key does not exist or does not have a timeout0

Behaviour

  • If the specified key has an expiration time, the PERSIST command will remove that expiration, ensuring the key remains in the database.
  • If the key does not have an expiration or if the key does not exist, the command returns 0 and no action is taken.
  • The value of the key remains unchanged. Only the expiration is affected.

Errors

  1. Wrong type of value or key:
    • Error Message: (error) WRONGTYPE Operation against a key holding the wrong kind of value
    • Occurs when attempting to use the command on a key that contains a non-string value or one that does not support expiration.
  2. No timeout to persist:
    • This is not an error but occurs when the key either does not exist or does not have an expiration time. The command will return 0 in such cases.

Example Usage

Removing Expiration from a Key

Terminal window
127.0.0.1:7379> SET mykey "Hello"
OK
Terminal window
127.0.0.1:7379> EXPIRE mykey 10
(integer) 1
Terminal window
127.0.0.1:7379> PERSIST mykey
(integer) 1
Terminal window
127.0.0.1:7379> TTL mykey
(integer) -1

Explanation:

  • The PERSIST command removes the expiration from mykey, and the TTL command confirms that the key no longer has a time-to-live (TTL) value (-1 indicates no expiration).
  1. SET mykey "Hello": Sets the value of mykey to “Hello”.
  2. EXPIRE mykey 10: Sets an expiration of 10 seconds on mykey.
  3. PERSIST mykey: Removes the expiration from mykey.
  4. TTL mykey: Returns -1, indicating that mykey does not have an expiration time.

Attempting to Persist a Non-Existent Key

Terminal window
127.0.0.1:7379> PERSIST mykey
(integer) 0

Explanation:

  • The command returns 0 because the key mykey does not exist in the database.

Persisting a Key Without Expiration

Terminal window
127.0.0.1:7379> SET mykey "Hello"
OK
Terminal window
127.0.0.1:7379> PERSIST mykey
(integer) 0

Explanation:

  • The command returns 0 because mykey does not have an expiration time set.

Conclusion

The PERSIST command is a useful tool for managing the lifecycle of keys in a DiceDB database. By removing the expiration from a key, you can ensure that the key remains in the database until explicitly deleted. This command is straightforward but powerful, allowing for greater control over key persistence.