Summary
Command Name | GETDEL |
Usage | Get string value and delete key |
Group | string |
ACL Category | @write @string @fast |
Time Complexity | O(1) |
Flag | READONLY FAST |
Arity | 2 |
Signature
GETDEL <key>
Usage
Get value and delete the key after that. It works similar to the GET command, just this deletes the key additionaly.
Arguments
Here is the description of what each part of the GETDEL command means in the above signature-
Parameter | Description | Name | Type |
---|---|---|---|
<key> | The key name | key | key |
Notes
- This command accepts only a single key. Multiple keys are not supported.
Return Value
List of values is returned for the provided keys.
Return value | Case for the return value | Type |
---|---|---|
The value of the key | When the key exists and is of type string. | string |
<nil> | When the key does not exist. | null |
error | When the data type of the key is not string. | error |
Notes
- This command throws an error/exception if the data type is not a string. So make sure to handle the exception if there is a possibility of your data being any other type.
Examples
Here are a few examples of the GETDEL command usage-
# Redis GETDEL command examples
# Set value for "sitename"
127.0.0.1:6379> set sitename bigboxcode
OK
# Get and delete key (and value) of "sitename"
127.0.0.1:6379> getdel sitename
"bigboxcode"
# Check if "sitename" still exists
# It will not exist as already deleted in the last step
127.0.0.1:6379> exists sitename
(integer) 0
# Try to apply GETDEL for a key that does not exist
127.0.0.1:6379> getdel wrongkey
(nil)
# Create a list and add items
127.0.0.1:6379> rpush users "John Done" "Second User" "Last User"
(integer) 3
# Check list
127.0.0.1:6379> lrange users 0 -1
1) "John Done"
2) "Second User"
3) "Last User"
# Try to apply GETDEL to data that is not of type string (list in this case)
# Will return an error, as GETDEL can be applied for string data type only
127.0.0.1:6379> getdel users
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- If the key data type is not a string the following error message is returned –
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Code Implementations
Here are the usage examples of the Redis GETEX command in different programming languages.
// Redis GETDEL command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
// Set value for "sitename"
// Command: set sitename bigboxcode
// Result: OK
commandResult, err := rdb.Set(ctx, "sitename", "bigboxcode", 0).Result()
if err != nil {
fmt.Println("Command: set sitename bigboxcode | Error: " + err.Error())
}
fmt.Println("Command: set sitename bigboxcode | Result: " + commandResult)
// Get and delete key (and value) of "sitename"
// Command: getdel sitename
// Result: "bigboxcode"
commandResult, err = rdb.GetDel(ctx, "sitename").Result()
if err != nil {
fmt.Println("Command: getdel sitename | Error: " + err.Error())
}
fmt.Println("Command: getdel sitename | Result: " + commandResult)
// Check if "sitename" still exists
// It will not exist as already deleted in the last step
// Command: exists sitename
// Result: (integer) 0
existCommandResult, err := rdb.Exists(ctx, "sitename").Result()
if err != nil {
fmt.Println("Command: exists sitename | Error: " + err.Error())
}
fmt.Printf("Command: exists sitename | Result: %vn", existCommandResult)
// Try to apply GETDEL for a key that does not exist
// Command: getdel wrongkey
// Result: (nil)
commandResult, err = rdb.GetDel(ctx, "sitename").Result()
if err != nil {
fmt.Println("Command: getdel wrongkey | Error: " + err.Error())
}
fmt.Println("Command: getdel wrongkey | Result: " + commandResult)
// Create a list and add items
// Command: rpush users "John Done" "Second User" "Last User"
// Result: (integer) 3
rpushCommandResult, err := rdb.RPush(ctx, "users", "John Done", "Second User", "Last User").Result()
if err != nil {
fmt.Println("Command: rpush users "John Done" "Second User" "Last User" | Error: " + err.Error())
}
fmt.Printf("Command: rpush users "John Done" "Second User" "Last User" | Result: %vn", rpushCommandResult)
// Try to apply GETDEL to data that is not of type string (list in this case)
// Will return an error, as GETDEL can be applied for string data type only
// Command: getdel users
// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
commandResult, err = rdb.GetDel(ctx, "sitename").Result()
if err != nil {
fmt.Println("Command: getdel users | Error: " + err.Error())
}
fmt.Println("Command: getdel users | Result: " + commandResult)
}
Output:
Command: set sitename bigboxcode | Result: OK
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: 0
Command: getdel wrongkey | Error: redis: nil
Command: getdel wrongkey | Result:
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: redis: nil
Command: getdel users | Result:
Notes
- Use the “GetDel” method of Go package, for using the Redis GETDEL command.
- The method signature is as below-
GetDel(ctx context.Context, key string) *StringCmd
// Redis GETDEL command example in JavaScript(NodeJS)
import { createClient } from 'redis';
// Create redis client
const redisClient = createClient({
url: 'redis://default:@localhost:6379'
});
redisClient.on('error', err => console.log('Error while connecting to Redis', err));
// Connect Redis client
await redisClient.connect();
/**
* Set value for "sitename"
*
* Command: set sitename bigboxcode
* Result: OK
*/
let commandResult = await redisClient.set('sitename', 'bigboxcode');
console.log("Command: set sitename bigboxcode | Result : " + commandResult);
/**
* Get and delete key (and value) of "sitename"
*
* Command: getdel sitename
* Result: "bigboxcode"
*/
commandResult = await redisClient.getDel('sitename');
console.log("Command: getdel sitename | Result : " + commandResult);
/**
* Check if "sitename" still exists
* It will not exist as already deleted in the last step
*
* Command: exists sitename
* Result: (integer) 0
*/
commandResult = await redisClient.exists('sitename');
console.log("Command: exists sitename | Result : " + commandResult);
/**
* Try to apply GETDEL for a key that does not exist
*
* Command: getdel wrongkey
* Result: (nil)
*/
commandResult = await redisClient.getDel('wrongkey');
console.log("Command: getdel wrongkey | Result : " + commandResult);
/**
* Create a list and add items
*
* Command: rpush users "John Done" "Second User" "Last User"
* Result: (integer) 3
*/
commandResult = await redisClient.rPush('users', "John Done");
commandResult = await redisClient.rPush('users', "Second User");
commandResult = await redisClient.rPush('users', "Last User");
console.log("Command: rpush users "John Done" "Second User" "Last User" | Result : " + commandResult);
/**
* Try to apply GETDEL to data that is not of type string (list in this case)
* Will return an error, as GETDEL can be applied for string data type only
*
* Command: getdel users
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.getDel('users');
console.log("Command: getdel users | Result : " + commandResult);
} catch (error) {
console.log(error);
}
process.exit(0);
Output:
Command: set sitename bigboxcode | Result : OK
Command: getdel sitename | Result : bigboxcode
Command: exists sitename | Result : 0
Command: getdel wrongkey | Result : null
Command: rpush users "John Done" "Second User" "Last User" | Result : 3
[ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use “getDel” function of the NodeJS package, for the GETDEL command usage.
// Redis GETDEL command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class Getdel {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Set value for "sitename"
*
* Command: set sitename bigboxcode
* Result: OK
*/
String commandResult = jedis.set("sitename", "bigboxcode");
System.out.println("Command: set sitename bigboxcode | Result: " + commandResult);
/**
* Get and delete key (and value) of "sitename"
*
* Command: getdel sitename
* Result: "bigboxcode"
*/
commandResult = jedis.getDel("sitename");
System.out.println("Command: getdel sitename | Result: " + commandResult);
/**
* Check if "sitename" still exists
* It will not exist as already deleted in the last step
*
* Command: exists sitename
* Result: (integer) 0
*/
boolean existsCommandResult = jedis.exists("sitename");
System.out.println("Command: exists sitename | Result: " + existsCommandResult);
/**
* Try to apply GETDEL for a key that does not exist
*
* Command: getdel wrongkey
* Result: (nil)
*/
commandResult = jedis.getDel("wrongkey");
System.out.println("Command: getdel wrongkey | Result: " + commandResult);
/**
* Create a list and add items
*
* Command: rpush users "John Done" "Second User" "Last User"
* Result: (integer) 3
*/
long listCommandResult = jedis.rpush("users", "John Done", "Second User", "Last User");
System.out.println("Command: rpush users "John Done" "Second User" "Last User" | Result: " + listCommandResult);
/**
* Try to apply GETDEL to data that is not of type string (list in this case)
* Will return an error, as GETDEL can be applied for string data type only
*
* Command: getdel users
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = jedis.getDel("users");
System.out.println("Command: getdel users | Result: " + commandResult);
} catch (Exception e) {
System.out.println("Command: getdel users | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: set sitename bigboxcode | Result: OK
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: false
Command: getdel wrongkey | Result: null
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the “getDel” method from the Jedis package, for using the Redis GETDEL command.
- Signature of the “getDel” method is-
public String getDel(final String key)
// Redis GETDEL command examples in C#
using StackExchange.Redis;
namespace Getdel
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Set value for "sitename"
*
* Command: set sitename bigboxcode
* Result: OK
*/
var setCcommandResult = rdb.StringSet("sitename", "bigboxcode");
Console.WriteLine("Command: set sitename bigboxcode | Result: " + setCcommandResult);
/**
* Get and delete key (and value) of "sitename"
*
* Command: getdel sitename
* Result: "bigboxcode"
*/
var getCommandResult = rdb.StringGetDelete("sitename");
Console.WriteLine("Command: getdel sitename | Result: " + getCommandResult);
/**
* Check if "sitename" still exists
* It will not exist as already deleted in the last step
*
* Command: exists sitename
* Result: (integer) 0
*/
var existsCommandResult = rdb.KeyExists("sitename");
Console.WriteLine("Command: exists sitename | Result: " + existsCommandResult);
/**
* Try to apply GETDEL for a key that does not exist
*
* Command: getdel wrongkey
* Result: (nil)
*/
getCommandResult = rdb.StringGetDelete("wrongkey");
Console.WriteLine("Command: getdel wrongkey | Result: " + getCommandResult);
/**
* Create a list and add items
*
* Command: rpush users "John Done" "Second User" "Last User"
* Result: (integer) 3
*/
var listCommandResult = rdb.ListRightPush("users", new RedisValue[] { "John Done", "Second User", "Last User" });
Console.WriteLine("Command: rpush users "John Done" "Second User" "Last User" | Result: " + listCommandResult);
/**
* Try to apply GETDEL to data that is not of type string (list in this case)
* Will return an error, as GETDEL can be applied for string data type only
*
* Command: getdel users
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
getCommandResult = rdb.StringGetDelete("users");
Console.WriteLine("Command: getdel users | Result: " + getCommandResult);
}
catch (Exception e)
{
Console.WriteLine("Command: getdel users | Error: " + e.Message);
}
}
}
}
Output:
Command: set sitename bigboxcode | Result: True
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: False
Command: getdel wrongkey | Result:
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the “StringGetDelete” method for using Redis GETDEL.
- Here is the signature of “StringGetDelete” method-
RedisValue StringGetDelete(RedisKey key, CommandFlags flags = CommandFlags.None)
<?php
// Redis GETDEL command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set value for "sitename"
*
* Command: set sitename bigboxcode
* Result: OK
*/
$commandResult = $redisClient->set("sitename", "bigboxcode");
echo "Command: set sitename bigboxcode | Result: " . $commandResult . "n";
/**
* Get and delete key (and value) of "sitename"
*
* Command: getdel sitename
* Result: "bigboxcode"
*/
$commandResult = $redisClient->getdel("sitename");
echo "Command: getdel sitename | Result: " . $commandResult . "n";
/**
* Check if "sitename" still exists
* It will not exist as already deleted in the last step
*
* Command: exists sitename
* Result: (integer) 0
*/
$commandResult = $redisClient->exists("sitename");
echo "Command: exists sitename | Result: " . $commandResult . "n";
/**
* Try to apply GETDEL for a key that does not exist
*
* Command: getdel wrongkey
* Result: (nil)
*/
$commandResult = $redisClient->getdel("wrongkey");
echo "Command: getdel wrongkey | Result: " . $commandResult . "n";
/**
* Create a list and add items
*
* Command: rpush users "John Done" "Second User" "Last User"
* Result: (integer) 3
*/
$commandResult = $redisClient->rpush("users", ["John Done", "Second User", "Last User"]);
echo "Command: rpush users "John Done" "Second User" "Last User" | Result: " . $commandResult . "n";
/**
* Try to apply GETDEL to data that is not of type string (list in this case)
* Will return an error, as GETDEL can be applied for string data type only
*
* Command: getdel users
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try{
$commandResult = $redisClient->getdel("users");
echo "Command: getdel users | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: getdel users | Error: " . $e->getMessage() . "n";
}
Output:
Command: set sitename bigboxcode | Result: OK
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: 0
Command: getdel wrongkey | Result:
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the “getdel” method from predis for the Redis GETDEL command.
- The method has a signature as below-
getdel(string $key): string
# Redis GETDEL command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Set value for "sitename"
# Command: set sitename bigboxcode
# Result: OK
commandResult = redisClient.set("sitename", "bigboxcode")
print("Command: set sitename bigboxcode | Result: {}".format(commandResult))
# Get and delete key (and value) of "sitename"
# Command: getdel sitename
# Result: "bigboxcode"
commandResult = redisClient.getdel("sitename")
print("Command: getdel sitename | Result: {}".format(commandResult))
# Check if "sitename" still exists
# It will not exist as already deleted in the last step
# Command: exists sitename
# Result: (integer) 0
commandResult = redisClient.exists("sitename")
print("Command: exists sitename | Result: {}".format(commandResult))
# Try to apply GETDEL for a key that does not exist
# Command: getdel wrongkey
# Result: (nil)
commandResult = redisClient.getdel("wrongkey")
print("Command: getdel wrongkey | Result: {}".format(commandResult))
# Create a list and add items
# Command: rpush users "John Done" "Second User" "Last User"
# Result: (integer) 3
commandResult = redisClient.rpush("users", "John Done", "Second User", "Last User")
print("Command: rpush users "John Done" "Second User" "Last User" | Result: {}".format(commandResult))
# Try to apply GETDEL to data that is not of type string (list in this case)
# Will return an error, as GETDEL can be applied for string data type only
# Command: getdel users
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.getdel("users")
print("Command: getdel users | Result: {}".format(commandResult))
except Exception as error:
print("Command: getdel users | Error: ", error)
Output:
Command: set sitename bigboxcode | Result: True
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: 0
Command: getdel wrongkey | Result: None
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- The “getdel” method of redisy-py, represents the Redis GETDEL command.
- The method has following signature-
def getdel(self, name: KeyT) -> ResponseT
# Redis GETDEL command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set value for "sitename"
# Command: set sitename bigboxcode
# Result: OK
commandResult = redis.set("sitename", "bigboxcode");
print("Command: set sitename bigboxcode | Result: ", commandResult, "n")
# Get and delete key (and value) of "sitename"
# Command: getdel sitename
# Result: "bigboxcode"
commandResult = redis.getdel("sitename");
print("Command: getdel sitename | Result: ", commandResult, "n")
# Check if "sitename" still exists
# It will not exist as already deleted in the last step
# Command: exists sitename
# Result: (integer) 0
commandResult = redis.exists("sitename");
print("Command: exists sitename | Result: ", commandResult, "n")
# Try to apply GETDEL for a key that does not exist
# Command: getdel wrongkey
# Result: (nil)
commandResult = redis.getdel("wrongkey");
print("Command: getdel wrongkey | Result: ", commandResult, "n")
# Create a list and add items
# Command: rpush users "John Done" "Second User" "Last User"
# Result: (integer) 3
commandResult = redis.rpush("users", ["John Done", "Second User", "Last User"]);
print("Command: rpush users "John Done" "Second User" "Last User" | Result: ", commandResult, "n")
# Try to apply GETDEL to data that is not of type string (list in this case)
# Will return an error, as GETDEL can be applied for string data type only
# Command: getdel users
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.getdel("users");
print("Command: getdel users | Result: ", commandResult, "n")
rescue => e
print("Command: getdel users | Error: ", e)
end
Output:
Command: set sitename bigboxcode | Result: OK
Command: getdel sitename | Result: bigboxcode
Command: exists sitename | Result: 0
Command: getdel wrongkey | Result:
Command: rpush users "John Done" "Second User" "Last User" | Result: 3
Command: getdel users | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- For using the Redis GETDEL command in Ruby, use the “getdel” method of redis-rb library.
- Signature of this method is –
def getdel(key)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
GET | Command Details |
GETDEL | Command Details |
MGET | Command Details |