# PUNSUBSCRIBE

> Unsubscribe from patterns.

Use `PUNSUBSCRIBE` to cancel pattern subscriptions of the current connection.

With no arguments the connection unsubscribes from every pattern it registered, otherwise only from the patterns named, which must be given exactly as they were passed to [`PSUBSCRIBE`](/redis/commands/pub-sub/psubscribe), since patterns are matched literally here and not expanded. The server sends one confirmation per pattern with the number of subscriptions still active.

Exact-channel subscriptions are not affected; cancel those with [`UNSUBSCRIBE`](/redis/commands/pub-sub/unsubscribe).

## Syntax

```redis
PUNSUBSCRIBE [<pattern> [<pattern> ...]]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<pattern>` | No | Yes | Glob-style channel pattern; omit to unsubscribe from all patterns. |

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

<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
PUNSUBSCRIBE 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.psubscribe("events:*");
await subscriber.punsubscribe("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.pSubscribe("events:*", console.log);
await subscriber.pUnsubscribe("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.psubscribe("events:*")
pubsub.punsubscribe("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.PSubscribe(ctx, "events:*")
    if err := pubsub.PUnsubscribe(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.psubscribe(new JedisPubSub() {
    @Override
    public void onPMessage(String pattern, String channel, String message) {
      punsubscribe();
    }
  }, "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.psubscribe("events:*")?;
    pubsub.punsubscribe("events:*")?;
    #[allow(unreachable_code)]
    Ok(())
}
```

</Accordion>

</AccordionGroup>
