Delete large cache entries
Removing large cached values without synchronously blocking Redis's main thread.
Redis · Module
UNLINK asynchronously deletes one or more Redis keys. It removes keys from the keyspace promptly while reclaiming their memory in a background thread.
UNLINK removes the specified keys from Redis's keyspace immediately, while memory reclamation runs asynchronously in another thread. It accepts one or more keys, ignores keys that do not exist, and returns how many keys were unlinked.
UNLINK key [key ...]| Name | Type | Description |
|---|---|---|
key [key ...] | key | One or more keys to delete asynchronously. |
Integer reply: the number of keys that were unlinked.
SET key1 "Hello"
SET key2 "World"
UNLINK key1 key2 key3Creates two keys, then asynchronously unlinks them; the missing third key is ignored.
Removing large cached values without synchronously blocking Redis's main thread.
Deleting several known keys while tolerating keys that may already be absent.
Performing non-blocking key removal in a clustered deployment after checking multi-key constraints.
UNLINK removes one or more keys without making memory reclamation block the main Redis thread. It unlinks each key from the keyspace and performs the heavier reclamation work asynchronously.
Follow-up: How does its complexity compare with DEL? UNLINK is O(1) per removed key regardless of value size in the command path, followed by O(N) reclamation work in another thread.
The command accepts one or more keys using the form UNLINK key [key ...]. Missing keys are ignored, and the integer reply reports how many keys were actually unlinked.
Follow-up: What does a reply of 2 mean for three supplied keys? Exactly two supplied keys existed and were unlinked; the third was absent or otherwise not counted.
UNLINK can remove multiple keys in one command, but clustered Redis environments impose multi-key behavior constraints. Keys may need to satisfy the cluster's requirements for multi-key operations.
Follow-up: What should be checked before issuing a multi-key UNLINK in a cluster? Review Redis's multi-key operation rules and ensure the selected keys are valid for the target cluster topology.
DELmulti-key operations