Skip to content

Instantly share code, notes, and snippets.

@ktkaushik
Created June 25, 2015 12:09
Show Gist options
  • Save ktkaushik/812071045dd43d5f8f94 to your computer and use it in GitHub Desktop.
Save ktkaushik/812071045dd43d5f8f94 to your computer and use it in GitHub Desktop.
LUA Eval script to Delete/Expire multiple keys in Redis
EVAL "for i, name in ipairs(redis.call('KEYS', 'author_*')) do redis.call('expire', name, 0); end" 0
@lucasmartins
Copy link

lucasmartins commented May 2, 2016

Thanks for that @ktkaushik!

A sample/example ruby code:

$redis.eval("for i, name in ipairs(redis.call('KEYS', 'author_*')) do redis.call('DEL', name); end")

@deepakkumarnd
Copy link

is there a way to pass the pattern as an argument @ktkaushik, @lucasmartins

@saubi1993
Copy link

@Kumarn33 use string interpolation my friend. !!

@aclk
Copy link

aclk commented Jun 11, 2019

127.0.0.1:6379> eval "return redis.call('del',unpack(redis.call('keys',ARGV[1])))" 0 'author_*'

@ktkaushik
Copy link
Author

Sorry guys, amidst the sea of notifications on GitHub, i missed these out completely.

@Rajat-Goyal
Copy link

Rajat-Goyal commented Dec 30, 2019

How would you modify this for a redis cluster? @ktkaushik @aclk
redis> EVAL "for i, name in ipairs(redis.call('KEYS', KEYS)) do redis.call('del', name); end" 2 m2, m3 fails with a

CROSSSLOT Keys in request don't hash to the same slot error

@Rajat-Goyal
Copy link

I am trying to delete multiple keys and not keys matching any particular pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment