The most common use case for deleting all keys from a Redis database using Golang is to clear the database during testing or development. It can also be useful when you want to flush stale data from your database.
The go-redis package provides the FlushDB
function that removes all keys from the currently selected database.
package main import ( "github.com/go-redis/redis/v8" "context" ) func main() { rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) err := rdb.FlushDB(context.Background()).Err() if err != nil { panic(err) } }
In the above code, we create a new client and connect to our local Redis server. Then we call the FlushDB
function to delete all keys.
FlushDB
function. It is always a good practice to handle errors in your code.Q: Can I use the FlushAll
function instead of FlushDB
?
A: Yes, but be aware that FlushAll
will remove data from all databases in the Redis instance, not just the currently selected one.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.