Using DEL and UNLINK Commands in Redis for Key Deletion
📖 Scenario: You are managing a Redis database that stores user session data for a web application. Sometimes, you need to delete session keys to free up memory or remove expired sessions.
🎯 Goal: Learn how to delete keys in Redis using the DEL and UNLINK commands. You will create keys, set a configuration variable, delete keys using both commands, and understand the difference between them.
📋 What You'll Learn
Create keys with specific names and values
Set a configuration variable to control deletion behavior
Use the DEL command to delete keys synchronously
Use the UNLINK command to delete keys asynchronously
💡 Why This Matters
🌍 Real World
Web applications often store session data in Redis. Efficiently deleting expired or unused sessions helps maintain performance and memory usage.
💼 Career
Understanding DEL and UNLINK commands is important for database administrators and backend developers managing Redis databases to optimize resource usage.
Progress0 / 4 steps
1
Create Redis keys for user sessions
Create three Redis keys named session:1001, session:1002, and session:1003 with values user1, user2, and user3 respectively using the SET command.
Redis
Hint
Use the SET command followed by the key name and value.
2
Set a configuration variable for lazy deletion
Set the Redis configuration variable lazyfree-lazy-eviction to yes using the CONFIG SET command to enable asynchronous deletion for eviction.
Redis
Hint
Use CONFIG SET followed by the variable name and value.
3
Delete keys synchronously using DEL
Use the DEL command to delete the keys session:1001 and session:1002 synchronously.
Redis
Hint
Use DEL followed by the keys you want to delete.
4
Delete keys asynchronously using UNLINK
Use the UNLINK command to delete the key session:1003 asynchronously.
Redis
Hint
Use UNLINK followed by the key you want to delete asynchronously.
Practice
(1/5)
1. What is the main difference between DEL and UNLINK commands in Redis?
easy
A. DEL deletes keys immediately and blocks Redis, while UNLINK deletes keys asynchronously in the background.
B. DEL deletes keys asynchronously, while UNLINK deletes keys immediately and blocks Redis.
C. DEL only deletes one key at a time, UNLINK deletes multiple keys.
D. DEL moves keys to a recycle bin, UNLINK permanently deletes keys.
Solution
Step 1: Understand DEL behavior
DEL removes keys immediately and blocks Redis until deletion finishes.
Step 2: Understand UNLINK behavior
UNLINK removes keys asynchronously in the background without blocking Redis.
Final Answer:
DEL deletes keys immediately and blocks Redis, while UNLINK deletes keys asynchronously in the background. -> Option A
Quick Check:
Deletion blocking = DEL [OK]
Hint: Remember: DEL blocks, UNLINK is non-blocking [OK]
Common Mistakes:
Thinking UNLINK blocks Redis like DEL
Believing DEL deletes keys asynchronously
Confusing number of keys each command can delete
2. Which of the following is the correct syntax to delete keys key1 and key2 using the UNLINK command in Redis?
easy
A. UNLINK (key1, key2)
B. UNLINK key1 key2
C. UNLINK [key1, key2]
D. UNLINK 'key1' 'key2'
Solution
Step 1: Recall Redis command syntax
Redis commands take keys as space-separated arguments without brackets or quotes.
Step 2: Match syntax for multiple keys
UNLINK key1 key2 correctly deletes both keys at once.
Final Answer:
UNLINK key1 key2 -> Option B
Quick Check:
Space-separated keys = correct syntax [OK]
Hint: Use space-separated keys without brackets or quotes [OK]
Common Mistakes:
Adding parentheses or brackets around keys
Using quotes around keys unnecessarily
Separating keys with commas
3. Given the following Redis commands executed in order: SET key1 "value1" SET key2 "value2" DEL key1 UNLINK key2 What keys remain in the database after these commands?
medium
A. No keys remain
B. Only key1 remains
C. Only key2 remains
D. Both key1 and key2 remain
Solution
Step 1: Analyze DEL key1
DEL key1 deletes key1 immediately.
Step 2: Analyze UNLINK key2
UNLINK key2 deletes key2 asynchronously, but it will be removed eventually.
Final Answer:
No keys remain -> Option A
Quick Check:
Both keys deleted = no keys left [OK]
Hint: Both DEL and UNLINK remove keys, so none remain [OK]
Common Mistakes:
Assuming UNLINK does not delete keys
Thinking UNLINK delays deletion indefinitely
Believing DEL only marks keys for deletion
4. You run the command DEL key1 key2 but get an error. Which of the following is the most likely cause?
medium
A. One or both keys do not exist
B. You need to use UNLINK instead of DEL
C. DEL cannot delete multiple keys at once
D. You used commas between keys like DEL key1, key2
Solution
Step 1: Check syntax for multiple keys
Redis commands require keys separated by spaces, not commas.
Step 2: Identify error cause
Using commas like DEL key1, key2 causes syntax error.
Final Answer:
You used commas between keys like DEL key1, key2 -> Option D
Quick Check:
Keys must be space-separated [OK]
Hint: Avoid commas; separate keys with spaces only [OK]
Common Mistakes:
Using commas between keys
Thinking DEL can't delete multiple keys
Confusing missing keys with syntax errors
5. You want to delete 1000 large keys in Redis without blocking the server. Which command and approach is best?
hard
A. Delete keys one by one with DEL in a loop
B. Use UNLINK with all 1000 keys at once
C. Use UNLINK on keys in small batches asynchronously
D. Use DEL with all 1000 keys at once
Solution
Step 1: Understand blocking behavior of DEL
DEL blocks Redis and deleting 1000 large keys at once can cause latency spikes.
Step 2: Use UNLINK in batches
UNLINK deletes keys asynchronously; batching avoids large memory spikes and keeps Redis responsive.
Final Answer:
Use UNLINK on keys in small batches asynchronously -> Option C
Quick Check:
Batch UNLINK = safe large deletion [OK]
Hint: Batch UNLINK to delete large keys without blocking [OK]
Common Mistakes:
Using DEL for large bulk deletes causing blocking
Deleting keys one by one causing slow performance
Using UNLINK without batching causing memory spikes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.