Skip to content

Instantly share code, notes, and snippets.

@polsala
Created July 17, 2023 11:48
Show Gist options
  • Save polsala/bb08983a01a8a9d308021b8a772e32a2 to your computer and use it in GitHub Desktop.
Save polsala/bb08983a01a8a9d308021b8a772e32a2 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