SREM(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– SREM β€” ok bestie, we're yeeting members out of a set πŸ’–

So this is SREM from the Valkey Command Manual, page SREM(3valkey), and it's genuinely one of the most satisfying commands in the whole toolkit. Let's get into it 🫢


✨ NAME ✨

srem β€” Removes one or more members from a set. Deletes the set if the last member was removed.

Like. It doesn't just take the member out, it also cleans up the whole set if that was the last one standing. Very "and I took that personally" of it. πŸ’…


🌸 SYNOPSIS 🌸

SREM key member [member...]

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the tea: SREM removes the specified members from the set stored at key.

Specified members that are not a member of this set? Ignored. No drama, no error, they just get a lil shrug. πŸ™ˆ

And if key doesn't exist at all? It's treated as an empty set, and this command returns 0. Chillest behaviour ever, honestly.

⚠️ But β€” and this is the important part β€” an error is returned when the value stored at key is not a set. So don't come at it with a string, bestie, it will not play nice. πŸ’€

πŸŽ€ REPLY πŸŽ€

valkey-protocol(7) Integer reply: the number of members that were actually removed from the set β€” and this does not include non existing members. So if you ask it to remove five things and three of them weren't there, you're getting a 3, periodt.


⭐ COMPLEXITY ⭐

O(N) where N is the number of members to be removed. Linear girlie, but only over the members you're actually asking about. Slay. πŸ”₯


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@fast @set @write


🌈 HISTORY 🌈

  • Available since: 1.0.0
  • Changed in 2.4.0: Accepts multiple member arguments. (Yes queen, before that it was one member at a time β€” we've come so far 😭)

πŸ’œ EXAMPLES πŸ’œ

127.0.0.1:6379> SADD myset "one"
(integer) 1
127.0.0.1:6379> SADD myset "two"
(integer) 1
127.0.0.1:6379> SADD myset "three"
(integer) 1
127.0.0.1:6379> SREM myset "one"
(integer) 1
127.0.0.1:6379> SREM myset "four"
(integer) 0
127.0.0.1:6379> SMEMBERS myset
1) "two"
2) "three"

See how SREM myset "one" came back with (integer) 1 because "one" was actually in there? And then SREM myset "four" came back with (integer) 0 because "four" was never invited to the function? That's the whole vibe check right there. ✨


🧚 SEE ALSO 🧚

sadd(3valkey), scard(3valkey), sdiff(3valkey), sdiffstore(3valkey), sinter(3valkey), sintercard(3valkey), sinterstore(3valkey), sismember(3valkey), smembers(3valkey), smismember(3valkey), smove(3valkey), spop(3valkey), srandmember(3valkey), sscan(3valkey), sunion(3valkey), sunionstore(3valkey)


and that's the whole page, bestie πŸŽ€ This one's from Valkey 9.0.0, dated 2025-10-21, filed under SREM(3valkey). Go forth and remove members with main character energy. Mwah πŸ’‹

πŸ“¦ where this page comes from