Skip to content

Instantly share code, notes, and snippets.

@shukla2112
Last active February 11, 2024 12:47
Show Gist options
  • Save shukla2112/3cdde41179dfb9ebcff8b92c281acdb3 to your computer and use it in GitHub Desktop.
Save shukla2112/3cdde41179dfb9ebcff8b92c281acdb3 to your computer and use it in GitHub Desktop.

delete the keys in redis with prefix

EVAL "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,5000 do \n redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) \n end \n return keys" 0 prefix:*

Ref - https://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis#comment39607023_16974060

List the client connected

redis-cli CLIENT LIST

Key Size of redis

https://gist.github.com/shukla2112/b9688800bf71f7be66e76943abecf729

Keys with no TTL

redis-cli --scan | while read LINE ; do TTL=`redis-cli ttl "$LINE"`; if [ $TTL -eq  -1 ]; then echo "$LINE"; fi; done;

Ref - https://stackoverflow.com/a/56265879

Useful commands

info keyspace
DBSIZE
# Sets the TTL for the key with prefix
import redis
r = redis.StrictRedis(host='<redis-host>', port=6379)
# iterate a list in batches of size n
# def batcher(iterable, n):
# args = [iter(iterable)] * n
# return izip_longest(*args)
# for keybatch in batcher(r.scan_iter('ratelimit-set:*'),500):
# ttl = r.ttl(*keybatch)
# if ttl == -1:
# print(*keybatch)
# r.delete(*keybatch)
count=0
for key in r.scan_iter("ratelimit-set:*"):
ttl = r.ttl(key)
if ttl == -1:
r.expire(key,600) # here 600 is in seconds.
count+=1
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment