# XSETID

> Set a stream's last generated ID and its bookkeeping counters.

URL: https://docs.hotkv.com/commands/xsetid/

```
XSETID key last-id [ENTRIESADDED entries-added] [MAXDELETEDID max-deleted-id]
```

- Group: Stream
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`XSETID` sets the last generated ID of the stream at `key`. Entries added with
`*` then get IDs above it. Replication and migration tools use it to recreate
a stream exactly.

## Options

| Option | Effect |
| --- | --- |
| `ENTRIESADDED entries-added` | Set the count of entries ever added to the stream (`entries-added` in XINFO STREAM). |
| `MAXDELETEDID max-deleted-id` | Set the highest ID ever deleted from the stream (`max-deleted-entry-id` in XINFO STREAM). |

The ID cannot be lower than the stream's current last entry, `ENTRIESADDED`
cannot be lower than the stream's length, and the ID cannot be lower than the
max deleted ID (the one given, or the current one).

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Set | Simple string `OK` | Simple string `OK` |

Error replies:

- `ERR no such key` if the stream does not exist.
- `ERR The ID specified in XSETID is smaller than the target stream top item`
- `ERR The entries_added specified in XSETID is smaller than the target stream length`
- `ERR The ID specified in XSETID is smaller than the provided max_deleted_entry_id`
- `ERR The ID specified in XSETID is smaller than current max_deleted_entry_id`
- `ERR entries_added must be positive` for a negative count.
- `ERR Invalid stream ID specified as stream command argument`, `ERR syntax error`, or `WRONGTYPE`.

## Examples

```
127.0.0.1:6379> XADD s 1700000000000-0 f v
"1700000000000-0"
127.0.0.1:6379> XSETID s 1800000000000-0
OK
127.0.0.1:6379> XADD s 1750000000000-0 f v
(error) ERR The ID specified in XADD is equal or smaller than the target stream top item
127.0.0.1:6379> XSETID s 1600000000000-0
(error) ERR The ID specified in XSETID is smaller than the target stream top item
```

## Compatibility

Same as Redis 7 and Valkey 8, including `ENTRIESADDED` and `MAXDELETEDID`.

## See also

[XADD](https://docs.hotkv.com/commands/xadd/index.md), [XINFO STREAM](https://docs.hotkv.com/commands/xinfo-stream/index.md), [XGROUP SETID](https://docs.hotkv.com/commands/xgroup-setid/index.md)