> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# HGETDEL

> Get and delete hash fields.

Use `HGETDEL` to read hash fields and delete them in the same atomic step.

The reply holds the previous value of each requested field, in the order requested, with null for fields that were not present. Reading and removing together removes the race that an [`HGET`](/docs/redis/commands/hash/hget) followed by an [`HDEL`](/docs/redis/commands/hash/hdel) would leave open, which makes the command a good fit for one-shot values such as one-time codes, claim tickets, or queued items keyed by name: exactly one caller gets the value.

`FIELDS <numfields>` introduces the field list and the count must match. The key is deleted when its last field is removed.

## Syntax

```redis theme={"system"}
HGETDEL <key> FIELDS <numfields> <field> [<field> ...]
```

## Arguments

| Argument                                   | Required | Repeatable | Description                                                               |
| ------------------------------------------ | -------- | ---------- | ------------------------------------------------------------------------- |
| `<key>`                                    | Yes      | No         | Redis key targeted by the command.                                        |
| `FIELDS <numfields> <field> [<field> ...]` | Yes      | No         | Fields to target. Give the field count first, then that many field names. |

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply                                                     |
| -------- | --------------------------------------------------------- |
| RESP2    | Array of bulk-string values or null values, one per field |
| RESP3    | Array of bulk-string values or null values, one per field |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    HGETDEL my-key FIELDS 1 field
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { Redis } from "@upstash/redis";

    const redis = Redis.fromEnv();

    // Set some hash fields
    await redis.hset("user:123", { name: "John", age: "30", email: "john@example.com" });

    // Get and delete specific fields
    const result = await redis.hgetdel("user:123", "name", "email");
    console.log(result); // { name: "John", email: "john@example.com" }

    // Verify fields were deleted
    const name = await redis.hget("user:123", "name");
    console.log(name); // null
    ```
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    ```python theme={"system"}
    from upstash_redis import Redis

    redis = Redis.from_env()
    result = redis.hgetdel("my-key", "field")
    print(result)
    ```
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.hgetdel("my-key", "FIELDS", "1", "field");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.hGetDel("my-key", "field");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.hgetdel("my-key", "field")
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        result, err := client.HGetDel(context.Background(), "my-key", "1", "field").Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      Object result = jedis.hgetdel("my-key", "1", "field");
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    use redis::TypedCommands;

    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let result = connection.hget_del("my-key", &["field"])?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [HGETDEL](/docs/redis/sdks/py/commands/hash/hgetdel.md)
- [HRANDFIELD](/docs/redis/commands/hash/hrandfield.md)
- [Changelog](/docs/redis/overall/changelog.md)
- [Overview](/docs/redis/sdks/py/commands/overview.md)
