Redis - Delete Multiple Keys by Pattern Using Lua Script
This guide provides a simple Lua script to delete multiple keys in Redis that match a specific pattern.
Usage
Run the script from within the Redis CLI.
Replace
PATTERN
in the script below with the actual key substring you want to delete in bulk.
eval "for _,k in ipairs(redis.call('keys','*PATTERN*')) do redis.call('del',k) end" 0
Explanation
The
eval
command executes the Lua script.redis.call('keys','*PATTERN*')
retrieves all keys matching the specified pattern.The
for
loop iterates through the list of keys and deletes each one usingredis.call('del',k)
.The
0
at the end indicates that no additional arguments are passed to the script.
02 May 2025