0
0
RedisHow-ToBeginner · 3 min read

How to Use DEL Command in Redis to Delete Keys

In Redis, use the DEL command to delete one or more keys from the database. The syntax is DEL key1 [key2 ...], which removes the specified keys if they exist.
📐

Syntax

The DEL command removes one or more keys from Redis. If a key does not exist, it is ignored.

  • DEL: The command to delete keys.
  • key1, key2, ...: One or more keys to delete.
redis
DEL key1 [key2 ...]
💻

Example

This example shows how to delete keys named user:1 and session:123 from Redis.

redis
SET user:1 "Alice"
SET session:123 "active"
DEL user:1 session:123
EXISTS user:1
EXISTS session:123
Output
OK OK (integer) 2 (integer) 0 (integer) 0
⚠️

Common Pitfalls

Common mistakes when using DEL include:

  • Trying to delete keys that do not exist, which returns 0 but causes no error.
  • Using DEL on many keys in a large database can block Redis and affect performance.
  • Confusing DEL with UNLINK, which deletes keys asynchronously.
redis
DEL non_existing_key

-- This returns 0 because the key does not exist, but no error occurs.

-- To avoid blocking on many keys, consider deleting keys in smaller batches or using UNLINK instead.
Output
(integer) 0
📊

Quick Reference

CommandDescriptionReturns
DEL key1 [key2 ...]Deletes specified keysNumber of keys deleted
EXISTS keyChecks if key exists1 if exists, 0 if not
UNLINK key1 [key2 ...]Deletes keys asynchronouslyNumber of keys unlinked

Key Takeaways

Use DEL followed by one or more keys to delete them from Redis.
DEL returns the number of keys it successfully deleted.
Deleting many keys at once with DEL can block Redis; use UNLINK for async deletion.
DEL ignores keys that do not exist without error.
Check key existence with EXISTS before or after deletion if needed.