0
0
Redisquery~5 mins

ZREM for removal in Redis

Choose your learning style9 modes available
Introduction

ZREM is used to remove one or more members from a sorted set in Redis. It helps keep your data clean by deleting items you no longer need.

You want to delete a specific user from a leaderboard.
You need to remove outdated tasks from a priority queue.
You want to clear certain items from a sorted list based on their names.
You want to update a set by removing old entries before adding new ones.
Syntax
Redis
ZREM key member [member ...]
The command removes one or more members from the sorted set stored at key.
If a member does not exist, it is simply ignored.
Examples
Removes the member 'user123' from the sorted set named 'leaderboard'.
Redis
ZREM leaderboard user123
Removes multiple members 'task1', 'task2', and 'task3' from the sorted set 'tasks'.
Redis
ZREM tasks task1 task2 task3
Sample Program

This example adds three users with scores to the 'leaderboard'. Then it removes 'user2'. Finally, it lists all remaining members with their scores.

Redis
ZADD leaderboard 100 user1 200 user2 150 user3
ZREM leaderboard user2
ZRANGE leaderboard 0 -1 WITHSCORES
OutputSuccess
Important Notes

ZREM returns the number of members that were removed.

If the key does not exist, ZREM returns 0.

Summary

ZREM removes specified members from a sorted set.

It ignores members that do not exist without error.

Use ZREM to keep your sorted sets updated and clean.