0
0
Redisquery~5 mins

LPOP and RPOP for removal in Redis

Choose your learning style9 modes available
Introduction

LPOP and RPOP remove items from a list in Redis. They help manage data by taking out elements from the start or end.

When you want to process tasks in the order they were added (queue behavior).
When you need to remove the oldest item from a list.
When you want to remove the newest item from a list.
When you want to consume messages from a list one by one.
When you want to keep a list size manageable by removing items from either end.
Syntax
Redis
LPOP key
RPOP key

LPOP removes and returns the first element of the list stored at key.

RPOP removes and returns the last element of the list stored at key.

Examples
Removes and returns the first element from the list named mylist.
Redis
LPOP mylist
Removes and returns the last element from the list named mylist.
Redis
RPOP mylist
Removes the oldest task from the tasks list.
Redis
LPOP tasks
Removes the newest message from the messages list.
Redis
RPOP messages
Sample Program

This example first adds three fruits to the list fruits. Then it removes the first fruit with LPOP and the last fruit with RPOP. Finally, it shows the remaining fruits.

Redis
LPUSH fruits apple banana cherry
LPOP fruits
RPOP fruits
LRANGE fruits 0 -1
OutputSuccess
Important Notes

If the list is empty or the key does not exist, LPOP and RPOP return null.

These commands modify the list by removing elements, so use them when you want to consume or discard items.

Summary

LPOP removes the first item from a list.

RPOP removes the last item from a list.

Both commands return the removed item or null if the list is empty.