# Error if channel is NOT empty

**URL:** <https://community.seqera.io/t/error-if-channel-is-not-empty/1592>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [January 7, 2025, 8:35pm UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592 "2025-01-07T20:35:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sstrong99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/sstrong99/32/188_2.png) [@sstrong99](https://community.seqera.io/u/sstrong99)\
**Post date:** [January 7, 2025, 8:35pm UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592/1 "2025-01-07T20:35:02Z")

</div>

I want to throw an error if a channel is not empty. I know i could pass the channel into a process that always errors, but i was hoping there was a little bit simpler of a method. For example, something similar to

```auto
Channel
  .fromPath("xyz/*")
  .ifEmpty {
      log.error("ERROR...")
      System.exit(2)
  }

```

but with the inverse behavior

---

<div class="post-metadata">

**Author:** ![yinshiyi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/yinshiyi/32/1091_2.png) [@yinshiyi](https://community.seqera.io/u/yinshiyi)\
**Post date:** [January 11, 2025, 12:30am UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592/2 "2025-01-11T00:30:27Z")

</div>

try this, it is not pretty, but works

The map part is needed otherwise the notEmpty error will execute with Empty channel input.

```auto
test_channel
        .ifEmpty {
            log.error("this is empty")
        }
        .first()
        .map { it ->
            if (it != null) {
                log.error("this is not empty")
            }
        }

```

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [January 13, 2025, 1:43pm UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592/3 "2025-01-13T13:43:08Z")

</div>

If a channel is empty, the following operator won’t do anything so simply add a `map` on the end of your channel.

```groovy
params.input = false
workflow {
    ch_in = params.input ? Channel.fromList([1, 2, 3]) : Channel.empty()
    ch_in.map{ error "Error: Channel is not empty" }
}

```

---

<div class="post-metadata">

**Author:** ![sstrong99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/sstrong99/32/188_2.png) [@sstrong99](https://community.seqera.io/u/sstrong99)\
**Post date:** [January 13, 2025, 9:06pm UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592/4 "2025-01-13T21:06:23Z")

</div>

thanks, that works great!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [January 20, 2025, 9:06pm UTC](https://community.seqera.io/t/error-if-channel-is-not-empty/1592/5 "2025-01-20T21:06:33Z")

</div>

Closed on January 20, 2025, 9:06pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
