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: https://redislabs.com/redis-enterprise/technology/gears/

  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.