89

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?

Abe Voelker
  • 30,124
  • 14
  • 81
  • 98

1 Answers1

131

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)
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 3
    Wow guess I should have tried that first, haha. For some reason I thought that command didn't apply to sets. Thanks! – Abe Voelker Jun 10 '11 at 02:45
  • 7
    yeah it took me a while to figure that one too.. the commands in [`Keys`](http://redis.io/commands#generic) apply globally to all keys regardless of data type. – Anurag Jun 10 '11 at 02:47
  • 9
    I see; I suppose that makes sense. Ah well, I'll leave this embarrassing question up for the next fool like me who didn't bother with the [tutorial](http://redis.io/topics/data-types-intro). :-) – Abe Voelker Jun 10 '11 at 02:50
  • And I would be that fool because why would you want to keep an empty set in memory... – Jonathan Leung Jun 08 '12 at 03:38
  • 6
    Because an empty set means something different from a non existent set. Consider: newuser111:questions (the set of questions by new user 111) vs. nonexistentuser:questions (supposedly the set of questions by nonexistentuser, a user that does not exist). It's useful to have an empty set to distinguish between the two. – Chris Pfohl Sep 23 '14 at 14:42