-
-
Save palerdot/0cdeef0747e07b84f4c34ea646157bb2 to your computer and use it in GitHub Desktop.
Delete Redis Stale Keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import redis | |
r = redis.StrictRedis(host='localhost', port=6379, db=0) | |
# To debug code on a single key you can use this instead of the for loops: | |
# key = r.randomkey() | |
# Delete all keys not accessed since 'idletime' | |
for key in r.scan_iter("*"): | |
idle = r.object("idletime", key) | |
# idle time is in seconds | |
if idle > 3600: | |
r.delete(key) | |
# Delete all keys without a TTL to expire | |
for key in r.scan_iter("*"): | |
# ttl is a type long or -1 if it is not set | |
if r.ttl(key) == -1: | |
r.delete(key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment