Is there any way to remove the Redis keys of completed jobs?

I am using Laravel powered Redis queues which I monitor with Horizon. I want to remove keys f completed or failed jobs. I searched a lot but could not find anything suitable. i found this Redis::command('flushdb'); but this flush all of my others keys too which will needed to complete queued job.

There are multiple options to execute bulk delete in Redis:

  1. executing cron to call redis-cli to delete the keys - below is an example

This command will delete all keys matching users:*
redis-cli --scan --pattern users:* | xargs redis-cli del

If you are in redis 4.0 or above, you can use the unlink command instead to delete keys in the background.

redis-cli --scan --pattern users:* | xargs redis-cli unlink

  1. using Redisgears -

RG.PYEXECUTE “GearsBuilder().filter(lambda x: int(x[‘value’][‘age’]) > 35).foreach(lambda x: execute(‘del’, x[‘key’])).run(‘user:*’)”
Reference: Triggers and Functions | Redis

  1. Client side logic to scan matching keys and delete them in batchs - you can run SCAN command to matching keynames and delete them in batches.
  2. SET key expiry - if you know that particular keys can be processed or failed should not live in Redis more than specific time, then you can set key EX accordingly

Note that depends on number of matching keys the overall process may take time and may impact your production.

Hope this helps.

on the subject of the keys there are many points to attack, a timeout can be established for a key, which is a limited time that has the same to be functional, when the life time has elapsed, the key is automatically destroyed This is the same as if the user calls the DEL command.

Another important point to keep in mind is that keys with expiration time can be created using commands such as SET.

set key 100 ex 10
ttl key
(integer) 9

Take all this into account since it is a topic of keys which can help to discard doubts that are feared regarding the elimination of keys and may be the starting point to the question of this topic.

Is there a reason why you aren’t removing the individual keys at the time of completion or failure? If you are able to cleanup as you go, then the entire issue is avoided so I’m curious what aspect of your system doesn’t allow this.

There is a way that we can flush 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, FLUSHALL can be the option.