Last active
March 3, 2023 18:45
-
-
Save zioproto/ac434a5067605a4f093d5925c8695ec3 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
Delete all keys without a TTL with Redis Sentinel