> ## 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.

# XGROUP

> Manage consumer groups.

Use `XGROUP` to manage the consumer groups of a stream.

`CREATE` registers a group and sets the position it starts reading from: `0` replays the whole stream from the beginning, while `$` delivers only entries added after the group was created. `MKSTREAM` creates the stream too when it does not exist yet, which avoids an error on a group created before the first producer runs. `SETID` moves that position afterwards, which is how a group is rewound for a replay or fast-forwarded past a backlog.

`CREATECONSUMER` and `DELCONSUMER` add and remove consumers explicitly; consumers are otherwise created on first read. Deleting a consumer returns how many pending entries went away with it, and those entries are then no longer pending for anyone, so hand them over with [`XCLAIM`](/docs/redis/commands/streams/xclaim) or [`XAUTOCLAIM`](/docs/redis/commands/streams/xautoclaim) first if the work still matters.

`DESTROY` removes a group along with all of its pending state, while leaving the stream and its entries untouched.

## Syntax

```redis theme={"system"}
XGROUP CREATE <key> <group> <id | $> [MKSTREAM]
XGROUP CREATECONSUMER <key> <group> <consumer>
XGROUP DELCONSUMER <key> <group> <consumer>
XGROUP DESTROY <key> <group>
XGROUP SETID <key> <group> <id | $>
```

## Arguments

| Argument     | Required   | Repeatable | Description                                                                  |
| ------------ | ---------- | ---------- | ---------------------------------------------------------------------------- |
| `subcommand` | Yes        | No         | Consumer-group operation shown in the syntax block.                          |
| `key`        | Yes        | No         | Stream key.                                                                  |
| `group`      | Yes        | No         | Consumer-group name.                                                         |
| `consumer`   | Some forms | No         | Consumer name for CREATECONSUMER or DELCONSUMER.                             |
| `id \| $`    | Some forms | No         | Starting or replacement last-delivered ID; `$` means the stream's latest ID. |
| `MKSTREAM`   | No         | No         | Create an empty stream when CREATE targets a missing key.                    |

## 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    | Simple string `OK` for `CREATE` and `SETID`; Integer for `CREATECONSUMER`, `DELCONSUMER`, and `DESTROY` |
| RESP3    | Simple string `OK` for `CREATE` and `SETID`; Integer for `CREATECONSUMER`, `DELCONSUMER`, and `DESTROY` |

<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"}
    XGROUP CREATE events workers $ MKSTREAM
    ```
  </Accordion>

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

    const redis = Redis.fromEnv();

    const result = await redis.xgroup("mystream", {
      type: "CREATE",
      group: "mygroup",
      id: "$",
      options: { MKSTREAM: true }
    });
    ```
  </Accordion>

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

    redis = Redis.from_env()
    result = redis.xgroup_create("events", "workers", "$", mkstream=True)
    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.xgroup("CREATE", "events", "workers", "$", "MKSTREAM");
    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.xGroupCreate("events", "workers", "$", { MKSTREAM: true });
    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.xgroup_create("events", "workers", "$", mkstream=True)
    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.XGroupCreateMkStream(context.Background(), "events", "workers", "$").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.xgroupCreate("events", "workers", redis.clients.jedis.StreamEntryID.LAST_ENTRY, true);
      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.xgroup_create_mkstream("events", "workers", "$")?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [XGROUP](/docs/redis/sdks/ts/commands/stream/xgroup.md)
- [XGROUP DELCONSUMER](/docs/redis/sdks/py/commands/stream/xgroup_delconsumer.md)
- [XGROUP DESTROY](/docs/redis/sdks/py/commands/stream/xgroup_destroy.md)
- [XGROUP CREATE](/docs/redis/sdks/py/commands/stream/xgroup_create.md)
- [XGROUP CREATECONSUMER](/docs/redis/sdks/py/commands/stream/xgroup_createconsumer.md)
