# Is there any way to cancel multiple tasks at once?

**URL:** https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755
**Category:** Ray Core
**Created:** [December 22, 2022, 7:21am UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755 "2022-12-22T07:21:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kyleqian](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kyleqian](https://discuss.ray.io/u/kyleqian)
#### Post date: [December 22, 2022, 7:21am UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755/1 "2022-12-22T07:21:41Z")

</div>

Hi, Is there any way to cancel multiple object reference at once? Here is the definition of ray.cancel:  
`def cancel(object_ref: "ray.ObjectRef", *, force: bool = False, recursive: bool = True):`  
So I cannot do this:

```auto
readies, rests = ray.wait(futures, timeout=10, num_returns=1)
ray.cancel(*rests)

```

I get an error:  
TypeError: cancel() takes 1 positional argument but 4 were given

---

<div class="post-metadata">

### Author: ![cade](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/cade/32/3837_2.png) [@cade](https://discuss.ray.io/u/cade)
#### Post date: [December 22, 2022, 9:18pm UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755/2 "2022-12-22T21:18:52Z")

</div>

Hi @kyleqian ! Currently `ray.cancel` takes only a single object ref. I think improving it to take a list of object refs is not a bad idea (feature request is here [[Core] `ray.cancel` multiple ObjectRefs · Issue #24559 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/24559)).

What kind of workload are you running? Would batching cancel requests speed it up in some way?

---

<div class="post-metadata">

### Author: ![kyleqian](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kyleqian](https://discuss.ray.io/u/kyleqian)
#### Post date: [December 24, 2022, 11:01pm UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755/3 "2022-12-24T23:01:39Z")

</div>

Thank you. I use Ray to process images and often need to cancel multiple tasks if one of them failed. Batching cancel requests could be have better performance than manual loop.

---

<div class="post-metadata">

### Author: ![cade](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/cade/32/3837_2.png) [@cade](https://discuss.ray.io/u/cade)
#### Post date: [December 27, 2022, 4:15am UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755/4 "2022-12-27T04:15:02Z")

</div>

I see. How big of a performance improvement would you have if you had the ability to cancel in batch? If it’s large then we can add that to the GitHub issue for prioritization 🙂

---

<div class="post-metadata">

### Author: ![kyleqian](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kyleqian](https://discuss.ray.io/u/kyleqian)
#### Post date: [December 28, 2022, 10:07am UTC](https://discuss.ray.io/t/is-there-any-way-to-cancel-multiple-tasks-at-once/8755/5 "2022-12-28T10:07:45Z")

</div>

When some tasks fail, dozens to hundreds of related tasks may be canceled. I think batching cancel request can reduce the number of RPCs, and it will make the code more concise.
