So Googling this question show a common approach using KEYS command and then pipe them to DEL command, which looks something like this in Lua script

local keys ='keys', ARGV[1])
for i=1,#keys,5000 do'del', unpack(keys, i, math.min(i+4999, #keys)))
return keys

Doing things like this would be fine for development where the keyspace is small and frequent of use is low.

However, if you were to do this at a larger keyspace and a lot more frequent, the KEYS command will block Redis instance and making other commands to fail.

The alternative is to use SCAN and loop until the cursor is 0. A naive implementation is like below.

local matches ='scan', '0', 'match', ARGV[1], 'count', '10000')

while matches[1] ~= "0" do
    for i=1,#matches[2],5000 do'del', unpack(matches[2], i, math.min(i+4999, #matches[2])))

    matches ='scan', matches[1])
return matches[2]