Raheel Hasan
Raheel Hasan

Reputation: 6023

Laravel Redis / How to you flush only a specific redis database?

As you know Redis could have several databases, in my laravel setup ive defined 3 of them and using 2 at least

'0' (default)
'1' (cache)
'2' (queue)

Thats what ive set in config/database.php

Furthermore, due to certain need I clear all cache using flushall() method:

@Cache::flush();
@Redis::flushall();

However, it also clears the queue jobs which is not what I want. So is there a way to only flush database 0 and database 1 and not database 2?

Upvotes: 3

Views: 3499

Answers (2)

Raza
Raza

Reputation: 3383

redis-cli -n [database number] flushdb

Upvotes: 0

Ersoy
Ersoy

Reputation: 9596

You may use select and flushdb commands together to flush only selected database.

Select the Redis logical database having the specified zero-based numeric index. New connections always use the database 0.

Delete all the keys of the currently selected DB.

The following will select the database 1 first and flush only 1.

Redis::select(1);
Redis::flushdb();

It is going to execute following commands in redis;

1596794331.068032 [0 127.0.0.1:55088] "SELECT" "1"
1596794331.071332 [1 127.0.0.1:55088] "FLUSHDB"

Upvotes: 6

Related Questions