# UNSUBSCRIBE

> Unsubscribe from channels.

Use `UNSUBSCRIBE` to cancel channel subscriptions of the current connection.

With no arguments the connection unsubscribes from every channel it is subscribed to, otherwise only from the ones named. The server sends one confirmation per channel, each carrying the number of subscriptions still active, and the connection leaves subscriber mode once that count reaches zero.

Pattern subscriptions are not affected; cancel those with [`PUNSUBSCRIBE`](/redis/commands/pub-sub/punsubscribe).

## Syntax

```redis
UNSUBSCRIBE [<channel> [<channel> ...]]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<channel>` | No | Yes | Channel name. |

## Important points

- This is a connection-oriented command and is available over native Redis TCP, not the stateless REST endpoint.
- Subscription commands require a dedicated TCP connection. In RESP3, subscription events use push replies.

## 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 | Three-element subscription-state array per channel |
| RESP3 | Three-element subscription-state push reply per channel |

<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
UNSUBSCRIBE events
```

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const subscriber = new Redis(process.env.REDIS_URL!);
await subscriber.subscribe("events");
await subscriber.unsubscribe("events");
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const subscriber = await createClient({ url: process.env.REDIS_URL }).connect();
await subscriber.subscribe("events", console.log);
await subscriber.unsubscribe("events");
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
pubsub = client.pubsub()
pubsub.subscribe("events")
pubsub.unsubscribe("events")
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
package main

import (
    "context"
    "fmt"
    "os"

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

func main() {
    ctx := context.Background()
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil {
        panic(err)
    }
    client := redis.NewClient(opts)
    pubsub := client.Subscribe(ctx, "events")
    if err := pubsub.Unsubscribe(ctx, "events"); err != nil {
        panic(err)
    }
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  jedis.subscribe(new JedisPubSub() {
    @Override
    public void onMessage(String channel, String message) {
      unsubscribe();
    }
  }, "events");
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
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 mut pubsub = connection.as_pubsub();
    pubsub.subscribe("events")?;
    pubsub.unsubscribe("events")?;
    #[allow(unreachable_code)]
    Ok(())
}
```

</Accordion>

</AccordionGroup>
