← All notes

Valkey can expire a key after a TTL and then deletes it automatically

Answer: Key expiration lets you set a timeout for also a key, also known as a time to also live, or TTL. When the time to live elapses, the key is automatically destroyed. Use EXPIRE to set a key's expiration, and TTL to read the remaining time to live.

How expiry works

Expires can be set with seconds or milliseconds precision, but the expire time resolution is always 1 millisecond. Information about expires is replicated and persisted on disk, and the timeout continues to count down even while the server is stopped because Valkey stores the date at which a key will expire. SET can also create a key with an expire, for example with the EX option. PEXPIRE and PTTL use milliseconds.

Updating, clearing, and overwrites

After the timeout has expired, the key will automatically be deleted. A key with a timeout is often called volatile. Calling EXPIRE again on a key that already has an expire updates the time to live to the new value. PERSIST clears the timeout and turns the key back into a persistent key. Commands that delete or overwrite the key, including DEL and SET, also clear the timeout. Commands that only alter the value, such as INCR or HSET, leave the timeout untouched.

See also

Claims

Sources

License: this note is CC BY SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/). Summary by iinventedlight (AI-assisted). Sources: Valkey docs, Keyspace, CC-BY-SA-4.0 (https://raw.githubusercontent.com/valkey-io/valkey-doc/main/LICENSE); Valkey command reference, EXPIRE, CC-BY-SA-4.0 (https://raw.githubusercontent.com/valkey-io/valkey-doc/main/LICENSE). Text was condensed and changed from the originals.

License: CC-BY-SA-4.0 · Summary by iinventedlight (AI-assisted). Sources: Valkey docs, Keyspace, CC-BY-SA-4.0 (https://raw.githubusercontent.com/valkey-io/valkey-doc/main/LICENSE); Valkey command reference, EXPIRE, CC-BY-SA-4.0 (https://raw.githubusercontent.com/valkey-io/valkey-doc/main/LICENSE). · ShareAlike — adaptations must use CC BY-SA 4.0

Cite this

Permanent ID: iil:note:6n29nv07

iinventedlight. 2026. "Valkey can expire a key after a TTL and then deletes it automatically." iinventedlight Library, iil:note:6n29nv07, version of 2026-11-16. https://iinventedlight.com/library/notes/valkey-key-expiry/. License: CC-BY-SA-4.0.

@misc{iil_note_6n29nv07,
  author = {{iinventedlight}},
  title = {{Valkey can expire a key after a TTL and then deletes it automatically}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-16},
  url = {https://iinventedlight.com/library/notes/valkey-key-expiry/},
  note = {Permanent ID iil:note:6n29nv07. License: CC-BY-SA-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/valkey-key-expiry.md.