3000 explained code solutions for 75 technologies


lua-redisHow to delete keys by pattern


local redis = (require 'redis').connect('127.0.0.1', 6379)
local keys = redis:scan(0, {match='t_*', count=100})[2]

for i,k in ipairs(keys) do
  redis:del(k)
endctrl + c
require 'redis'

load Redis module for Lua

connect

connect to Redis server

'127.0.0.1', 6379

Redis host and port to connect to

t_*

pattern to delete keys on

for i,k in ipairs(keys) do

iterate through found keys

del

deletes specified key


Usage example

local redis = (require 'redis').connect('127.0.0.1', 6379)

redis:set('t_1', 1)
redis:set('t_2', 2)
redis:set('t_3', 3)
local keys = redis:scan(0, {match='t_*', count=100})[2]

for i,k in ipairs(keys) do
  redis:del(k)
end

print(redis:get('t_1'))
print(redis:get('t_2'))
print(redis:get('t_3'))
output
nil
nil
nil