DEL

DEL key [key ...]
Available since:
Redis Open Source 1.0.0
Time complexity:
O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
ACL categories:
@keyspace, @write, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.

Removes the specified keys. A key is ignored if it does not exist.

Required arguments

key [key ...]

One or more keys to delete.

Examples

Foundational: Delete one or more keys using DEL (ignores non-existent keys, returns count of deleted keys)
> SET key1 "Hello"
OK
> SET key2 "World"
OK
> DEL key1 key2 key3
(integer) 2
res = r.set("key1", "Hello")
print(res)
# >>> True

res = r.set("key2", "World")
print(res)
# >>> True

res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK

const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK

const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2
console.log(await redis.set('key1', 'Hello')); // >>> OK
console.log(await redis.set('key2', 'World')); // >>> OK

const delResult = await redis.del('key1', 'key2', 'key3');
console.log(delResult); // >>> 2
        String delResult1 = jedis.set("key1", "Hello");
        System.out.println(delResult1); // >>> OK

        String delResult2 = jedis.set("key2", "World");
        System.out.println(delResult2); // >>> OK

        long delResult3 = jedis.del("key1", "key2", "key3");
        System.out.println(delResult3); // >>> 2
            CompletableFuture<Void> delExample = asyncCommands.set("key1", "Hello")
                    .thenCompose(r1 -> {
                        System.out.println(r1);              // >>> OK
                        return asyncCommands.set("key2", "World");
                    })
                    .thenCompose(r2 -> {
                        System.out.println(r2);              // >>> OK
                        return asyncCommands.del("key1", "key2", "key3");
                    })
                    .thenAccept(r3 -> {
                        System.out.println(r3);              // >>> 2
                    })
                    .toCompletableFuture();
            Mono<Void> delExample = reactiveCommands.set("key1", "Hello")
                    .flatMap(r1 -> {
                        System.out.println(r1);              // >>> OK
                        return reactiveCommands.set("key2", "World");
                    })
                    .flatMap(r2 -> {
                        System.out.println(r2);              // >>> OK
                        return reactiveCommands.del("key1", "key2", "key3");
                    })
                    .doOnNext(r3 -> {
                        System.out.println(r3);              // >>> 2
                    })
                    .then();
	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(delResult1) // >>> OK

	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(delResult2) // >>> OK

	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(delResult3) // >>> 2
    reply = redisCommand(c, "SET key1 Hello");
    printf("%s\n", reply->str);
    // >>> OK
    freeReplyObject(reply);

    reply = redisCommand(c, "SET key2 World");
    printf("%s\n", reply->str);
    // >>> OK
    freeReplyObject(reply);

    reply = redisCommand(c, "DEL key1 key2 key3");
    printf("%lld\n", reply->integer);
    // >>> 2
    freeReplyObject(reply);
        bool delResult1 = db.StringSet("key1", "Hello");
        Console.WriteLine(delResult1);  // >>> true

        bool delResult2 = db.StringSet("key2", "World");
        Console.WriteLine(delResult2);  // >>> true

        long delResult3 = db.KeyDelete(["key1", "key2", "key3"]);
        Console.WriteLine(delResult3);  // >>> 2
        echo $r->set('key1', 'Hello') . PHP_EOL;             // >>> OK
        echo $r->set('key2', 'World') . PHP_EOL;             // >>> OK

        $delResult = $r->del('key1', 'key2', 'key3');
        echo $delResult . PHP_EOL;                           // >>> 2
        if let Ok(res) = r.set("key1", "Hello") {
            let res: String = res;
            println!("{res}");    // >>> OK
        }

        if let Ok(res) = r.set("key2", "World") {
            let res: String = res;
            println!("{res}");    // >>> OK
        }

        match r.del(&["key1", "key2", "key3"]) {
            Ok(res) => {
                let res: i32 = res;
                println!("{res}");    // >>> 2
            },
            Err(e) => {
                println!("Error deleting keys: {e}");
                return;
            }
        }
        if let Ok(res) = r.set("key1", "Hello").await {
            let res: String = res;
            println!("{res}");    // >>> OK
        }

        if let Ok(res) = r.set("key2", "World").await {
            let res: String = res;
            println!("{res}");    // >>> OK
        }

        match r.del(&["key1", "key2", "key3"]).await {
            Ok(res) => {
                let res: i32 = res;
                println!("{res}");    // >>> 2
            },
            Err(e) => {
                println!("Error deleting keys: {e}");
                return;
            }
        }

Redis Software and Redis Cloud compatibility

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

Return information

Integer reply: the number of keys that were removed.
RATE THIS PAGE
Back to top ↑