Skip to content

Instantly share code, notes, and snippets.

@guilleJB
Forked from polsala/clean_redis_keys.py
Created October 25, 2024 11:36
Show Gist options
  • Select an option

  • Save guilleJB/d85a21c80922f2a4565d33a5d8d4eebd to your computer and use it in GitHub Desktop.

Select an option

Save guilleJB/d85a21c80922f2a4565d33a5d8d4eebd to your computer and use it in GitHub Desktop.
Clean not used redis keys since x seconds
def clean_idle_redis_keys(redis_host='192.168.0.4', redis_port=6379, db=0, startswithkey='rq', idletime=604800):
import redis
from tqdm import tqdm
r = redis.StrictRedis(host=redis_host, port=redis_port, db=db)
for key in tqdm(r.scan_iter("*")):
idle = r.object("idletime", key)
# idle time is in seconds.
if idle > idletime and key.startswith(startswithkey):
print("Deleting {}".format(key))
r.delete(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment