Difficulty: Advanced
What is the difference between DEL and UNLINK in Redis, and when does it actually matter?
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.
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.
| Aspect | DEL | UNLINK |
|---|---|---|
| Key disappears from keyspace | Immediately | Immediately |
| Memory freed | Synchronously | In background for large values |
| Cost on main thread | O(N) in elements | Roughly O(1) per key |
| Return value | Number of keys removed | Number of keys removed |
| Available since | Always | Redis 4.0 |
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.
SCAN.UNLINK; INFO memory shows lazyfree_pending_objects while work is queued.maxmemory briefly.UNLINK does not fix the real problem of having giant keys. Find them with redis-cli --bigkeys or --memkeys and split them.DEL or UNLINK requires all keys in one hash slot.# 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.
UNLINK, Lazy Freeing, Big Keys, lazyfree Settings, FLUSHALL ASYNC