Skip to content

ZREM

The ZREM command is used to remove one or more specified members from a sorted set. Non-existent members are ignored.

If the key does not exist, it is treated as an empty sorted set and 0 is returned.

The time complexity of the ZREM command is O(M*log(N)), where N is the number of elements in the sorted set and M is the number of members to be removed.

Terminal window
ZREM key member [member ...]

Parameter Description

  • key: The key of the sorted set
  • member: One or more members to remove

The ZREM command removes specified members from a sorted set. If the specified members do not exist in the sorted set, they are ignored. If the sorted set does not exist, it is treated as an empty sorted set.

The command returns the number of members successfully removed, excluding non-existent members.

Terminal window
redis> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis> ZREM myzset "two"
(integer) 1
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
redis> ZREM myzset "four"
(integer) 0

In redisun, the ZREM command is implemented through the ZRemCommand class and the zrem method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setHost("localhost");
options.setPort(6379);
});
// Add some test data
redisun.zadd("myzset", 1.0, "one");
redisun.zadd("myzset", 2.0, "two");
redisun.zadd("myzset", 3.0, "three");
// Remove a single member
long removedCount = redisun.zrem("myzset", "two");
// Remove multiple members
long removedCount2 = redisun.zrem("myzset", "one", "three");
// Asynchronously remove members
CompletableFuture<Long> future = redisun.asyncZrem("myzset", "two");
// Handle asynchronous result
future.thenAccept(removedCount -> {
System.out.println("Removed " + removedCount + " members");
});
  1. Non-existent members are ignored and no error is generated
  2. If the key does not exist, it is treated as an empty sorted set
  3. The command returns the number of members successfully removed