DEL vs. UNLINK

Difficulty: Advanced

Question

What is the difference between DEL and UNLINK in Redis, and when does it actually matter?

Answer

Both commands remove keys from the keyspace immediately, so a following GET sees nothing. The difference is when memory is freed: DEL frees the value synchronously on the main thread, while UNLINK (added in Redis 4.0) detaches the key and hands large values to a background thread to free. For a small string there is no real difference, but deleting a hash or sorted set with millions of elements via DEL can block Redis for a noticeable time, while UNLINK returns almost instantly.

Why freeing memory can block

Redis executes commands on a single main thread. Freeing a collection means walking every element and releasing each allocation. A sorted set with 5 million members means millions of free() calls, which can take long enough that every other client sees a latency spike.

UNLINK does the cheap part, removing the key from the keyspace, on the main thread. It then estimates the cost of freeing the value. If the value is small, it is freed immediately anyway, since handing off would cost more. If it is large, the value is queued for a background lazyfree thread.

Comparison

AspectDELUNLINK
Key disappears from keyspaceImmediatelyImmediately
Memory freedSynchronouslyIn background for large values
Cost on main threadO(N) in elementsRoughly O(1) per key
Return valueNumber of keys removedNumber of keys removed
Available sinceAlwaysRedis 4.0

Related lazy-free settings

Redis also deletes data internally, and each path has a switch to make it lazy:

  • lazyfree-lazy-user-del: make DEL behave like UNLINK.
  • lazyfree-lazy-eviction: free evicted keys in the background.
  • lazyfree-lazy-expire: free expired keys in the background.
  • lazyfree-lazy-server-del: for implicit deletes, such as RENAME or SET overwriting a big key.
  • replica-lazy-flush: flush the replica's old dataset in the background during full sync.
  • lazyfree-lazy-user-flush: make FLUSHALL and FLUSHDB default to async.

In Redis 7.x these all default to no, but defaults have been changing in newer releases and forks, so check with CONFIG GET lazyfree* on your version. You can also choose explicitly with FLUSHALL ASYNC or FLUSHDB ASYNC.

When it matters in practice

  • Removing big keys: a leaderboard sorted set, a huge list used as a queue, or a session hash that grew unbounded.
  • Bulk deletes during cache invalidation, combined with SCAN.
  • Evicting or expiring large values under memory pressure, where synchronous freeing would add latency exactly when the server is busiest.

Gotchas

  • Memory does not drop instantly after UNLINK; INFO memory shows lazyfree_pending_objects while work is queued.
  • If you delete faster than the background thread can free, memory can overshoot maxmemory briefly.
  • UNLINK does not fix the real problem of having giant keys. Find them with redis-cli --bigkeys or --memkeys and split them.
  • In Redis Cluster, multi-key DEL or UNLINK requires all keys in one hash slot.
UNLINK on a big sorted set. The key vanishes immediately; the expensive freeing happens off the main thread.
  1. Client → Main thread: UNLINK leaderboard:2025
  2. Main thread → Main thread: Remove key from keyspace
  3. Main thread → Lazyfree thread: Queue 5M members for freeing
  4. Main thread → Client: (integer) 1, returns at once
  5. Lazyfree thread → Lazyfree thread: Free memory in background

Code examples

Deleting a big key without blocking

# Find big keys first
redis-cli --bigkeys

# Size of the key in bytes
redis-cli MEMORY USAGE leaderboard:2025

# Non-blocking delete
redis-cli UNLINK leaderboard:2025

# Watch background freeing progress
redis-cli INFO memory | grep lazyfree_pending_objects

# Make DEL and expiry lazy too, then check current values
redis-cli CONFIG SET lazyfree-lazy-user-del yes lazyfree-lazy-expire yes
redis-cli CONFIG GET 'lazyfree*'

# Async flush of a whole database
redis-cli FLUSHDB ASYNC

UNLINK returns almost immediately even for very large values. lazyfree_pending_objects shows how many objects are still waiting to be freed.

Key points

  • DEL and UNLINK both remove the key from the keyspace immediately.
  • DEL frees memory on the main thread; UNLINK frees large values in a background thread.
  • Small values are freed synchronously even with UNLINK because it is cheaper.
  • lazyfree-* settings make eviction, expiry, DEL and server-side deletes lazy.
  • FLUSHALL ASYNC and FLUSHDB ASYNC avoid blocking on full flushes.
  • UNLINK hides the symptom; giant keys are still a design smell.

Common follow-up questions

  • Why is Redis mostly single-threaded and what work does it push to background threads?
  • How would you find and break up big keys?
  • What happens to memory if you UNLINK keys faster than the background thread can free them?
  • Should you set lazyfree-lazy-user-del to yes by default?

Interview tips

  • Make it clear both commands remove the key immediately; only memory freeing differs.
  • Tie the answer to Redis's single-threaded command execution.
  • Mention the lazyfree config family and FLUSHALL ASYNC to show depth.

Concepts covered

UNLINK, Lazy Freeing, Big Keys, lazyfree Settings, FLUSHALL ASYNC

More Redis & Caching questions