Redis: How to delete all keys older than 3 months

redis delete all keys
redis expireat
redis expire key
redis sadd expire
redis hset expire
redis set expire on all keys
redis iterate over all keys
jedis expire

I want to flush out all keys older than 3 months. These keys were not set with an expire date.

Or if that is not possible, can I then delete maybe the oldest 1000 keys?

Are you NOW using an expire? If so, you could loop through all keys if no TTL is set then add one.

Python example:

for key in redis.keys('*'):
    if redis.ttl(key) == -1:
        redis.expire(key, 60 * 60 * 24 * 7)
        # This would clear them out in a week

EDIT As @kouton pointed out use scan over keys in production, see a discussion on that at: SCAN vs KEYS performance in Redis

Delete redis keys that have been inactive for 30 days, You may make take the advantage of OBJECT IDLETIME command, which returns the number of seconds since the object stored at the specified key is idle (​not  In Redis you can flush cache/database and delete all keys from all databases or from the particular database only using FLUSHALL and FLUSHDB commands. To delete all keys from all Redis databases, use the FLUSHALL command. To delete all keys of the selected Redis database only, use the FLUSHDB commnad. Cool Tip: List all keys from the all

A bit late, but check out the OBJECT command. There you will find object idle time (with 10 second resolution). It's used for debugging purposes but still, could be a nice workaround for your need.


🧚 ‍ Redis: how to delete all keys older than 3 months, Are you using expiration now? If so, you can skip all the keys if TTL is not installed, then add one. Python example: for key in redis.keys('*'): if  And the FLUSHALL command deletes all keys in all databases. We can execute these operations in a background thread using the ASYNC option. This is useful if the flush takes a long time, as making the command ASYNC stops it from blocking until its complete. We should note that the ASYNC option is available from Redis 4.0.0. 3. Delete Using the

Using the object idletime you can delete all keys that have not been used since three months. It is not exactly what you ask. If you created a key 6 months ago, but the key is accessed everyday, then idletime is updated and this script will not delete it. I hope the script can help:

#!/usr/bin/env python
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
for key in r.scan_iter("*"):
    idle = r.object("idletime", key)
    # idle time is in seconds. This is 90days
    if idle > 7776000:

Redis: How to delete all keys older than 3 months -, i want flush out keys older 3 months. these keys not set expire date. or if not possible, can delete maybe oldest 1000 keys? are now using  Method 1: Delete by Means of Outlook Auto-Archive. To begin with, launch Outlook application and in “Mail” pane, select the mail folder whose emails older than a specific period you would like to delete. Then right click the folder and choose “Properties” from its context menu. Next a new dialog box of “<Folder> Properties” will pop up.

Sorry, that's not possible, as stated in the comments above. In Redis it's crucial to create your own indexes to support your access patterns.

Tip: What you should do is to create a sorted set (ZADD) with all new or modified keys, and set the score to a timestamp. This way you can with ease fetch the keys within a time period using ZRANGEBYSCORE.

If you want to expire your existing keys, get all keys (expensive) and set a TTL for each using the EXPIRE command.

EXPIRE key seconds, After the timeout has expired, the key will automatically be deleted. the contents of the key, including DEL, SET, GETSET and all the *STORE commands​. In Redis versions prior 2.1.3 altering a key with an expire set using a command absolute Unix timestamps (in milliseconds in case of Redis version 2.6 or greater). The time-complexity for this operation is O(N), N being the number of keys in all existing databases. *FLUSHALL ASYNC (Redis 4.0.0 or greater) Redis is now able to delete keys in the background in a different thread without blocking the server.

Found and cleanup keys without expiration in Redis, Where keys is the total number of key and expires the number of keys with What the simplest way to finding the keys without listing all the keys? can create a script for deleting the keys cat without_ttl | awk '{print "redis-cli del "$1}' > $redis.with do |redis| redis.set redis_key, info, ttl: 3.days end. In Redis, we can delete all keys with a prefix easily by DEL command when we want to delete a bunch of keys that have a common pattern. X Up to date information from Bobcares regarding COVID-19

Find and Delete multiple keys matching by a pattern in Redis: The , When the count of found keys reaches 100(will differ for other apps), then only we are executing all the delete commands queued earlier. Finally when stream  I've installed a web app called scrumblr which uses redis as a database. What I am attempting to do is delete all keys that have been inactive for 30 days, or have not been accessed in 30 days. I ran . redis-cli KEYS* Which returns all of the keys, though it does not show a timestamp.

Deleting A Huge Number of Keys In Redis - Elliot Chance, The first step is to create a file with all of the keys to be deleted. We exported this from our database, and it's just a text file with one key per line,  Two possibilities: Keep track of all stored keys and do the filtering in your clients. Or use expires and deal with stale caches. Whatever you do, if you reach for pattern-matching operations in Redis you should look for an alternative in the way you do it or something other than Redis.

  • Unfortunately, it's not possible. But you can delete Redis keys according to the data associated in them.
  • @LeonidBeschastny Thanks. Is it because keys are not stored in a sorted way or saved with a date?
  • There is no way to get the time of keys creation unless you store it yourself in the data associated with it. As for the oldest keys, Redis optimized for fast key-value access, so it stores its data as a hash table.
  • Never use keys in production; use scan instead.
  • Is there any way to execute this command from StackExchange.Redis.Extention Api?
  • I used the idletime to delete everything older than 1 hour as follows: