Maybe I'm just blind, but I don't see an explicit set command in Redis for emptying an existing set (without emptying the entire database). For the time being, I'm doing a set difference on the set with itself and storing it back into itself:
redis> SMEMBERS metasyn 1) "foo" 2) "bar" redis> SDIFFSTORE metasyn metasyn metasyn (integer) 0 redis> SMEMBERS metasyn (empty list or set)
But that looks a little silly... is there a better way to do this?
Redis Commands There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.
The easiest way to clear Redis cache is to use the redis-cli command. Databases in Redis are stored individually. Using the redis-cli command allows you to either clear the keys from all databases, or from a single specified database only.
In Redis 4.0, there is a new command UNLINK to delete the keys in Redis memory. This command is very similar to DEL: it removes the specified keys. Just like DEL a key is ignored if it does not exist. However the command performs the actual memory reclaiming in a different thread, so it is not blocking, while DEL is.
You could delete the set altogether with DEL
.
DEL metasyn
From redis console,
redis> SMEMBERS metasyn 1) "foo" 2) "bar" redis> DEL metasyn (integer) 1 redis> SMEMBERS metasyn (empty list or set)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With