Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Module

GETDEL

GETDEL returns a string value and deletes its key in the same command. It returns nil when the key is missing or does not contain a string.

Since
6.2.0

How it works

GETDEL atomically retrieves a key's string value and removes that key when the operation succeeds. If the key is absent or holds a non-string value, it returns a nil response and does not provide a value.

Syntax

GETDEL key

Parameters

NameTypeDescription
keykeyThe name of the key to retrieve and then delete.

Returns

Bulk string reply containing the key's value, or a nil reply when the key does not exist or its value is not a string.

Key points

  • GETDEL combines reading and deletion for one key.
  • Unlike GET, a successful GETDEL removes the key after retrieving its value.
  • The required argument is the key name whose value should be returned and removed.
  • For an existing string key, the result is the stored value as a bulk string.

Common mistakes

  • Deletion applies only when the key contains a string; a non-string value produces a nil response.
  • Missing keys and keys with another data type both return nil, so callers must distinguish nil from an empty string.

Example: Read and remove a session value

SET session:42 "user-123"
GETDEL session:42
GET session:42

The first command stores a string, GETDEL returns it while deleting the key, and the final GET returns nil.

When to use it

One-time token consumption

Consume one-time tokens, then remove them after successful retrieval.

Read-and-invalidate caching

Fetch a transient cache entry while ensuring it cannot be read again.

Single-use work items

Atomically take ownership of a temporary string-valued work item.

Interview questions

QWhat does GETDEL do?

GETDEL takes one key, returns its string value, and deletes the key on success. It combines retrieval and removal in one Redis command.

Follow-up: What happens for a hash key? GETDEL returns nil because the value type is not a string, rather than returning the hash contents.

QWhat is the command syntax and input?

The syntax is GETDEL key. The key identifies the value to read and then delete.

Follow-up: How would you delete several keys? Use DEL with multiple key arguments, because GETDEL accepts only one key.

QWhat replies can GETDEL return?

A bulk string reply contains the value. A nil response means the key is absent or its value is not a string.

Follow-up: Why should a client not treat nil as an empty string? Redis uses nil to represent the missing or incompatible-type cases, while an existing empty string is still a string value.

  • GET
  • DEL
  • GETEX

Source: GETDEL | Docs. Checked 2026-10-16.