Deleting a large number of keys in Redis can cause latency spikes or blocked clients if handled with synchronous commands like DEL or KEYS. This guide explains how to perform large-scale deletions safely across Redis Software, Redis Cloud, and Redis OSS without affecting performance. It includes guidance on Environment-Specific Behavior, explains Why Massive Key Deletion Impacts Performance, outlines Recommended Methods for Bulk Deletion using non-blocking operations like UNLINK and SCAN, provides detailed Step-by-Step Instructions for both CLI and Redis Insight, and concludes with Troubleshooting and Best Practices to ensure efficient cleanup and resource recovery in production environments.
Environment-Specific Behavior
| Environment | Special Considerations |
|---|---|
| Redis Cloud | Same semantics as Redis Software. Both DEL and UNLINK are supported. In Active-Active (CRDB) databases, multi-key deletions that span hash slots fail with CROSSSLOT errors. Use the per-key UNLINK workaround below. |
| Redis Software |
UNLINK, SCAN, and pipelining are fully supported. In multi-shard clusters, run deletions per master node to avoid cross-slot operations. |
| Redis Software (CRDB) | Both DEL and UNLINK are synchronous and coordinated across regions. Use single-key deletion loops or the file-based UNLINK script for large datasets. |
| Redis OSS / Redis Stack | Behaves the same as Redis Software. Use SCAN and UNLINK for safe, non-blocking deletions. Avoid KEYS in production. |
Why Massive Key Deletion Impacts Performance
DEL is synchronous and blocks the main thread, so large deletions can cause latency spikes and stalled clients.KEYS scans the full keyspace and should never be used in production.
Multi-key delete behavior depends on database type. Standard Redis Software clustered databases allow multi-key DEL or UNLINK when keys share a hash slot.
Active-Active CRDBs do not, and return:
CROSSSLOT Keys in request don't hash to the same slot
See clustering behavior for multi-key operations: Redis Clustering Overview
Recommended Methods for Bulk Deletion
1. Use the UNLINK Command (Recommended)UNLINK is an asynchronous alternative to DEL.
It removes keys immediately from the keyspace and reclaims memory in the background.
Always use UNLINK for bulk deletions in production environments.
2. Use SCAN to Find Keys by PatternSCAN is a cursor-based, non-blocking iterator for safely listing keys.
Combine SCAN with UNLINK to delete keys matching a pattern.
Avoid using KEYS in production for this purpose.
3. Use Redis Insight Bulk Actions
Redis Insight provides a Bulk Actions tool that deletes matching keys using non-blocking operations.
Recommended for interactive cleanup or validation before deletion.
Step-by-Step Instructions
A. Command-Line (redis-cli) Method
Delete keys matching a pattern safely:
redis-cli -h <HOST> -p <PORT> --scan --pattern 'your:pattern:*' -i 0.01 | xargs redis-cli -h <HOST> -p <PORT> unlink
Explanation:
-
<HOST>and<PORT>are the endpoint of your database. -
--scan --patternsafely iterates through keyspace. -
-i 0.01adds a 10 ms delay between operations. -
xargsbatches deletions; use-L <number>to limit keys perUNLINKcall.
CRDB-Safe Method (Avoid CROSSSLOT Errors):
redis-cli -h <HOST> -p <PORT> --scan --pattern <PATTERN> -i 0.01 \
| sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\t/\\t/g' \
-e 's/^/UNLINK "/' -e 's/$/"/' \
| redis-cli -h <HOST> -p <PORT>This method writes all matching keys to a file, prefixes them with UNLINK, and executes them one by one to ensure cross-slot safety.
Python Example (redis-py, RedisCluster):
from redis.cluster import RedisCluster
def delete_keys_by_pattern(pattern="user:*", scan_count=1000, pipe_batch=1000):
cursor = "0"
while True:
cursor, keys = rc.scan(cursor=cursor, match=pattern, count=scan_count)
if keys:
for i in range(0, len(keys), pipe_batch):
batch = keys[i:i+pipe_batch]
pipe = rc.pipeline()
for k in batch:
pipe.unlink(k)
pipe.execute()
if cursor == "0":
breakThis pattern-based deletion works across Redis Software standalone, clustered, and CRDB databases when the OSS API is enabled.
B. Redis Insight Bulk Actions
Redis Insight bulk deletion works on both standalone and CRDB databases.
- Open Redis Insight and connect to your database.
- Navigate to Browser → Tree/List View.
- Filter by key name or type.
- Open Bulk Actions → Delete Keys.
- Review the list before deletion.
- Click Delete to execute safely.
Quick Fix Table
| Symptom | Likely Cause | Quick Fix |
|---|---|---|
| CROSSSLOT Keys in request don't hash to the same slot | Multi-key delete across slots | Run per-key delete using the CRDB-safe script |
| High latency during delete | Too many keys per batch | Reduce batch size, add delay with -i
|
| Keys not deleted or reappear |
SCAN incomplete or pattern too narrow |
Verify SCAN cursor completes (returns 0), adjust pattern |
Troubleshooting Common Issues
| Problem | Cause | Solution |
|---|---|---|
| High latency during delete | Batch size too large | Reduce batch size, add delay using -i
|
| Keys remain undeleted |
SCAN incomplete or narrow match |
Ensure cursor returns “0”; validate pattern |
| Memory not reclaimed | Fragmentation or tombstones (CRDB) | Run MEMORY PURGE or restart during maintenance |
| CROSSSLOT error | Multi-key deletes span slots | Delete per shard or use CRDB-safe command |
| Permission denied | ACL restrictions | Verify role allows UNLINK or DEL
|
| Redis Insight bulk deletion stuck | Too many keys selected | Use CLI method for large datasets |
| Deleted keys not freeing memory | Expired keys or fragmentation | Review memory optimization settings |
Best Practices
- Always prefer
UNLINKoverDELfor large deletions. - Never use
KEYSin production. - Run deletions per shard or node in clustered setups.
- For CRDB, delete keys individually or with per-key scripts.
- Monitor latency, ops/sec, and memory usage during deletions.
- Use
MEMORY PURGEafter large deletes to reclaim space. - Schedule major deletions during low-traffic or maintenance windows.
- Use Redis Insight Bulk Actions for visual safety and confirmation.
Troubleshooting Checklist
- Confirm you’re not using
KEYSorDELon large keyspaces. - Validate ACL permissions for deletion commands.
- If memory isn’t freed, check for fragmentation or CRDB tombstones.
- In CRDB, ensure all deleted keys belong to the same hash slot or use per-key deletion.
- Use Redis Insight Profiler,
SLOWLOG, or Prometheus to monitor during deletion.
0 comments
Please sign in to leave a comment.