RPOP

RPOP key [count]
Available since:
Redis Open Source 1.0.0
Time complexity:
O(N) where N is the number of elements returned
ACL categories:
@write, @list, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

Removes and returns the last elements of the list stored at key.

By default, the command pops a single element from the end of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.

Required arguments

key

The name of the key that holds the list.

Optional arguments

count

The number of elements to pop. Without it, a single element is popped.

Examples

Foundational: Remove and return the last element(s) from a list using RPOP (supports optional count parameter to pop multiple elements from tail)
redis> RPUSH mylist "one" "two" "three" "four" "five"
(integer) 5
redis> RPOP mylist
"five"
redis> RPOP mylist 2
1) "four"
2) "three"
redis> LRANGE mylist 0 -1
1) "one"
2) "two"
res18 = r.rpush("mylist", *["one", "two", "three", "four", "five"])
print(res18) # >>> 5

res19 = r.rpop("mylist")
print(res19) # >>> "five"

res20 = r.rpop("mylist", 2)
print(res20) # >>> ['four', 'three']

res21 = r.lrange("mylist", 0, -1)
print(res21)  # >>> [ "one", "two" ]

const res21 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
console.log(res21); // 5

const res22 = await client.rPop('mylist');
console.log(res22); // 'five'

const res23 = await client.rPopCount('mylist', 2);
console.log(res23); // [ 'four', 'three' ]

const res24 = await client.lRange('mylist', 0, -1);
console.log(res24); // [ 'one', 'two' ]

        long rPopResult1 = jedis.rpush(
            "mylist", "one", "two", "three", "four", "five"
        );
        System.out.println(rPopResult1); // >>> 5

        String rPopResult2 = jedis.rpop("mylist");
        System.out.println(rPopResult2); // >>> five

        List<String> rPopResult3 = jedis.rpop("mylist", 2);
        System.out.println(rPopResult3); // >>> [four, three]

        List<String> rPopResult4 = jedis.lrange("mylist", 0, -1);
        System.out.println(rPopResult4); // >>> [one, two]
            CompletableFuture<Void> rpop = asyncCommands.rpush("mylist", "one", "two", "three", "four", "five")
                    .thenCompose(res18 -> {
                        System.out.println(res18); // >>> 5

                        return asyncCommands.rpop("mylist");
                    }).thenCompose(res19 -> {
                        System.out.println(res19); // >>> five

                        return asyncCommands.rpop("mylist", 2);
                    }).thenCompose(res20 -> {
                        System.out.println(res20); // >>> [four, three]

                        return asyncCommands.lrange("mylist", 0, -1);
                    })
                    .thenAccept(res21 -> System.out.println(res21)) // >>> [one, two]
                    .toCompletableFuture();
            Mono<Void> rpop = reactiveCommands.rpush("mylist", "one", "two", "three", "four", "five").doOnNext(res18 -> {
                System.out.println(res18); // >>> 5
            }).flatMap(res18 -> reactiveCommands.rpop("mylist")).doOnNext(res19 -> {
                System.out.println(res19); // >>> five
            }).flatMap(res19 -> reactiveCommands.rpop("mylist", 2).collectList()).doOnNext(res20 -> {
                System.out.println(res20); // >>> [four, three]
            }).flatMap(res20 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res21 -> {
                System.out.println(res21); // >>> [one, two]
            }).then();
	rPushResult, err := rdb.RPush(ctx, "mylist",
		"one", "two", "three", "four", "five",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(rPushResult) // >>> 5

	rPopResult, err := rdb.RPop(ctx, "mylist").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(rPopResult) // >>> five

	rPopCountResult, err := rdb.RPopCount(ctx, "mylist", 2).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(rPopCountResult) // >>> [four three]

	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult) // >>> [one two]
        long rPopResult1 = db.ListRightPush("mylist", ["one", "two", "three", "four", "five"]);
        Console.WriteLine(rPopResult1); // >>> 5

        RedisValue rPopResult2 = db.ListRightPop("mylist");
        Console.WriteLine(rPopResult2); // >>> five

        RedisValue[] rPopResult3 = db.ListRightPop("mylist", 2);
        Console.WriteLine($"[{string.Join(", ", rPopResult3)}]");
        // >>> [four, three]

        RedisValue[] rPopResult4 = db.ListRange("mylist", 0, -1);
        Console.WriteLine($"[{string.Join(", ", rPopResult4)}]");
        // >>> [one, two]
        $res21 = $r->rpush('mylist', 'one', 'two', 'three', 'four', 'five');
        echo $res21 . PHP_EOL;
        // >>> 5

        $res22 = $r->rpop('mylist');
        echo $res22 . PHP_EOL;
        // >>> five

        $res23 = $r->rpop('mylist', 2);
        echo json_encode($res23) . PHP_EOL;
        // >>> ["four","three"]

        $res24 = $r->lrange('mylist', 0, -1);
        echo json_encode($res24) . PHP_EOL;
        // >>> ["one","two"]

Give these commands a try in the interactive console:

RPUSH mylist "one" "two" "three" "four" "five" RPOP mylist RPOP mylist 2 LRANGE mylist 0 -1

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active

Return information

One of the following:

  • Nil reply: if the key does not exist.
  • Bulk string reply: when called without the count argument, the value of the last element.
  • Array reply: when called with the count argument, a list of popped elements.

History

  • Starting with Redis version 6.2.0: Added the count argument.
RATE THIS PAGE
Back to top ↑