Redis is an open-source, in-memory key-value data store. Redis keys are persistent by default, meaning that the Redis server will continue to store them unless they are deleted manually. There may, however, be cases where you’ve set a key but you know you will want to delete it after a certain amount of time has passed; in other words, you want the key to be volatile. This tutorial explains how to set keys to expire, check the time remaining until a key’s expiration, and cancel a key’s expiration setting.
This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.
The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli
, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.
Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.
You can also use an interactive terminal that is embedded on this page to experiment with the sample Redis commands in this tutorial. Click the following Launch an Interactive Terminal!
button to get started.
You can set an expiration time for an existing key with the expire
command, which takes the name of the key and the number of seconds until expiration as arguments. To demonstrate this, run the following two commands. The first creates a string key named key_melon
with a value of "cantaloupe"
, and the second sets it to expire after 450 seconds:
- set key_melon "cantaloupe"
- expire key_melon 450
If the timeout was set successfully, the expire
command will return (integer) 1
. If setting the timeout failed, it will instead return (integer) 0
.
Alternatively, you can set the key to expire at a specific point in time with the expireat
command. Instead of the number of seconds before expiration, it takes a Unix timestamp as an argument. A Unix timestamp is the number of seconds since the Unix epoch, or 00:00:00 UTC on January 1, 1970. There are a number of tools online you can use to find the Unix timestamp of a specific date and time, such as EpochConverter or UnixTimestamp.com.
For example, to set key_melon
to expire at 8:30pm GMT on May 1, 2025 (represented by the Unix timestamp 1746131400
), you could use the following command:
- expireat key_melon 1746131400
Note that if the timestamp you pass to expireat
has already occurred, it will delete the key immediately.
Any time you set a key to expire, you can check the time remaining until expiry (in seconds) with ttl
, which stands for “time to live”:
- ttl key_melon
Output(integer) 433
For more granular information, you can run pttl
which will instead return the amount of time until a key expires in milliseconds:
- pttl key_melon
Output(integer) 431506
Both ttl
and pttl
will return (integer) -1
if the key hasn’t been set to expire and (integer) -2
if the key does not exist.
If a key has been set to expire, any command that overwrites the contents of a key — like set
or getset
— will clear a key’s timeout value. To manually clear a key’s timeout, use the persist
command:
- persist key_melon
The persist
command will return (integer) 1
if it completed successfully, indicating that the key will no longer expire.
This guide details a number of commands used to manipulate and check key persistence in Redis. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.
For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign upRedis is an open-source, in-memory key-value data store. A NoSQL database, Redis doesn’t use structured query language, otherwise known as SQL. Redis instead comes with its own set of commands for managing and accessing data.
The tutorials included in this series cover a broad range of Redis commands, but they generally focus on connecting to a Redis database, managing a variety of data types, and troubleshooting and debugging problems, along with a few other more specific functions. They are written in cheat sheet format with self-contained examples. We encourage you to jump to whichever guide is relevant to the task you’re trying to complete.