Redis wildcard DEL as Lua script in Ruby


In Redis, KEYS returns keys matching a pattern while DEL removes the specified key. It is not possible with DELto remove keys matching a pattern.

Redis 2.6 introduces an ability to atomically execute Lua scripts using EVAL. This feature can be used to write a version of DEL that accepts wildcard patterns. Below in Ruby flavour, it removes all keys starting with 444:.

require 'redis'

RedisConn =
RedisConn.eval(<<-LUA, [], ["441:*"])
local keys ='keys', ARGV[1])
for i=1,#keys,5000 do'del', unpack(keys, i, math.min(i+4999, #keys)))
return keys