# Filtering and Sampling

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: Filtering and Sampling
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: connect/cookbooks/filtering
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: connect/cookbooks/filtering.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/develop/pages/connect/cookbooks/filtering.adoc
description: Configure Redpanda Connect to conditionally drop messages.
page-git-created-date: "2024-09-09"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/develop/connect/cookbooks/filtering.md -->

Filtering events in Redpanda Connect is both easy and flexible, this cookbook demonstrates a few different types of filtering you can do. All of these examples make use of the [`mapping` processor](https://docs.redpanda.com/cloud-data-platform/develop/connect/components/processors/mapping/) but shouldn’t require any prior knowledge.

## [](#the-basic-filter)The basic filter

Dropping events with [Bloblang](https://docs.redpanda.com/cloud-data-platform/develop/connect/guides/bloblang/about/) is done by mapping the function `deleted()` to the `root` of the mapped document. To remove all events indiscriminately you can simply do:

```yaml
pipeline:
  processors:
  - mapping: root = deleted()
```

But that’s most likely not what you want. We can instead only delete an event under certain conditions with a [`match`](https://docs.redpanda.com/cloud-data-platform/develop/connect/guides/bloblang/about/#pattern-matching) or [`if`](https://docs.redpanda.com/cloud-data-platform/develop/connect/guides/bloblang/about/#conditional-mapping) expression:

```yaml
pipeline:
  processors:
  - mapping: |
      root = if @topic.or("") == "foo" ||
        this.doc.type == "bar" ||
        this.doc.urls.contains("https://www.benthos.dev/").catch(false) {
        deleted()
      }
```

The above config removes any events where:

-   The metadata field `topic` is equal to `foo`

-   The event field `doc.type` (a string) is equal to `bar`

-   The event field `doc.urls` (an array) contains the string `https://www.benthos.dev/`


Events that do not match any of these conditions will remain unchanged.

## [](#sample-events)Sample events

Another type of filter we might want is a sampling filter, we can do that with a random number generator:

```yaml
pipeline:
  processors:
  - mapping: |
      # Drop 50% of documents randomly
      root = if random_int() % 2 == 0 { deleted() }
```

We can also do this in a deterministic way by hashing events and filtering by that hash value:

```yaml
pipeline:
  processors:
  - mapping: |
      # Drop ~10% of documents deterministically (same docs filtered each run)
      root = if content().hash("xxhash64").slice(-8).number() % 10 == 0 {
         deleted()
      }
```