0
0
Redisquery~5 mins

DEL and UNLINK for deletion in Redis

Choose your learning style9 modes available
Introduction
DEL and UNLINK are commands to remove keys from Redis. They help clean up data you no longer need.
When you want to delete a key and its value immediately.
When you want to delete keys without blocking Redis for a long time.
When cleaning up expired or temporary data.
When you want to free memory by removing unused keys.
When you want to delete multiple keys at once.
Syntax
Redis
DEL key [key ...]
UNLINK key [key ...]
DEL removes keys synchronously, blocking Redis until done.
UNLINK removes keys asynchronously, freeing Redis quickly.
Examples
Deletes the key named 'mykey' immediately.
Redis
DEL mykey
Deletes multiple keys at once.
Redis
DEL key1 key2 key3
Removes 'mykey' asynchronously without blocking Redis.
Redis
UNLINK mykey
Asynchronously deletes multiple keys.
Redis
UNLINK key1 key2
Sample Program
Sets a key 'mykey', deletes it with DEL, then checks if it exists.
Redis
SET mykey "hello"
DEL mykey
EXISTS mykey
OutputSuccess
Important Notes
DEL returns the number of keys it deleted.
UNLINK also returns the number of keys it scheduled for deletion.
Use UNLINK for large keys to avoid blocking Redis.
Summary
DEL deletes keys immediately and blocks Redis during deletion.
UNLINK deletes keys in the background without blocking.
Both commands accept multiple keys to delete at once.