A single Pub/Sub topic often carries several kinds of messages. An orders topic might carry orders from different countries, a logs topic might mix errors with routine notices, and an events topic might serve several teams at once. Without filtering, every subscription receives everything, and each subscriber has to throw away what it does not need.
Message filtering moves that decision into Pub/Sub. You attach a filter to a subscription, and only the messages that match are delivered to it. The rest are acknowledged automatically and never reach your code. Your subscribers get simpler, your traffic drops, and one topic can feed many different consumers, each seeing only its own slice.
There is one thing to understand before you write a filter. Pub/Sub filters look at message attributes, the small key and value labels you attach when you publish. They do not look inside the message body, and they do not support regular expressions. Many people discover that only after their filter matches nothing. This guide explains the rules clearly, then shows you how to create a filtered subscription in the console with the original screenshots kept in place, and with gcloud. We then cover the filter syntax with examples, a test, the limits, common mistakes and a short FAQ.
| Point | Supported | Not supported |
|---|---|---|
| What it reads | Message attributes | The message data, which is the body |
| Matching | Exact match, key exists, starts with | Regular expressions |
| Logic | AND, OR, NOT with parentheses | Custom code |
| Length | Up to 256 bytes | Longer filters |
| Changing it later | Create a new subscription | Editing the filter in place |
Because filters read attributes, the publisher has to put the information you want to filter on into attributes. If a country code lives only inside the message body, the filter cannot see it. Plan the attributes at the publishing side first.
Prerequisites:
Menu labels move from time to time, so treat the wording below as a guide.
1. Sign in to the Google Cloud console. Open the navigation menu, choose Pub/Sub and then Subscriptions.
2. Click Create subscription.

3. Enter a subscription ID, such as orders-india-sub.
4. Select the topic the subscription should read from.

5. Choose the delivery type, either pull or push.
6. Set the message retention duration and the subscription expiration period.
7. Set the acknowledgement deadline.

8. Find the subscription filter field and enter your filter, for example attributes.country = “IN”. Remember that you cannot change it after creating the subscription.

9. Click Create. The filtered subscription appears in the list.

gcloud pubsub subscriptions create orders-india-sub –topic=orders-topic –message-filter=’attributes.country = “IN”‘
For a push subscription, add the endpoint flag:
gcloud pubsub subscriptions create orders-india-push –topic=orders-topic –push-endpoint=https://my-service.example.com/push –message-filter=’attributes.country = “IN”‘
Wrap the whole filter in single quotes and use double quotes around the value, as shown, so your shell does not mangle it.
Here are the patterns you will use most, written for a message attribute named domain.
| Filter | Description |
|---|---|
attributes:domain |
Messages with the domain attribute |
NOT attributes:domain |
Messages without the domain attribute |
attributes.domain = "com" |
Messages with the domain attribute and the value of com |
attributes.domain != "com" |
Messages without the domain attribute and the value of com |
hasPrefix(attributes.domain, "co") |
Messages with the domain attribute and a value that starts with co |
NOT hasPrefix(attributes.domain, "co") |
Messages without the domain attribute and a value that starts with co |
You can combine conditions. The words AND, OR and NOT must be written in capital letters, and you should use parentheses whenever you mix AND with OR:
attributes:domain AND (attributes.domain = “net” OR attributes.domain = “org”)
A few rules to remember:
Publish one message that should match and one that should not, with attributes set:
gcloud pubsub topics publish orders-topic –message=”order 1″ –attribute=country=IN
gcloud pubsub topics publish orders-topic –message=”order 2″ –attribute=country=US
gcloud pubsub subscriptions pull orders-india-sub –auto-ack –limit=10
You should see order 1 only. If nothing arrives, check that the attribute name and value match your filter exactly. Matching is case sensitive.
A filter cannot be edited. Google documents a safe way to replace one without losing messages:
Message filtering lets one topic serve many subscribers without each one wading through everything. Put the values you need into attributes at the publishing side, write the filter carefully, test it with one matching and one non matching message, and remember that you cannot edit it later.