# How to recycle cycles in a canister that are about to be deleted

**URL:** <https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [November 7, 2024, 1:52am UTC](https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989 "2024-11-07T01:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haida](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@haida](https://forum.dfinity.org/u/haida)\
**Post date:** [November 7, 2024, 1:52am UTC](https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989/1 "2024-11-07T01:52:03Z")

</div>

Since we have deployed the contract to a relatively congested subnet, we plan to redeploy the contract to a relatively idle subnet before the business is fully started.

Question:

1. How should we reclaim the cycles in canister before calling `IC.delete_canister(xxx)` to delete the sub-canister?
2. When deleting the main canister and want to return its cycles balance, is it correct to use the command and argument `dfx canister delete --withdraw-cycles-to-dank main_canister` ?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [November 7, 2024, 8:39am UTC](https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989/2 "2024-11-07T08:39:54Z")

</div>

> [@haida](#):
>
> How should we reclaim the cycles in canister before calling `IC.delete_canister(xxx)` to delete the sub-canister?

You need to make the to-be-deleted-canister send as many cycles out as possible on its own. You can’t withdraw from outside of the canister.

> [@haida](#):
>
> When deleting the main canister and want to return its cycles balance, is it correct to use the command and argument `dfx canister delete --withdraw-cycles-to-dank main_canister` ?

Do you have a reason to use the `--withdraw-cycles-to-dank` flag and know what it does? Otherwise I would drop that

---

<div class="post-metadata">

**Author:** ![haida](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@haida](https://forum.dfinity.org/u/haida)\
**Post date:** [November 7, 2024, 10:10am UTC](https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989/3 "2024-11-07T10:10:03Z")

</div>

> [@Vivienne](#):
>
> You need to make the to-be-deleted-canister send as many cycles out as possible on its own. You can’t withdraw from outside of the canister.

I don’t quite understand what you mean. Do you mean to use the` ic.deposit_cycles(xxx)` function to reverse the transfer?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [November 7, 2024, 10:23am UTC](https://forum.dfinity.org/t/how-to-recycle-cycles-in-a-canister-that-are-about-to-be-deleted/36989/4 "2024-11-07T10:23:20Z")

</div>

Maybe a scenario makes it easier to discuss. Assume canister A wants to delete canister B but also wants to recover as many cycles as possible. You want to do it roughly this way:

1. A installs new code to B that knows how to send cycles
  - Of course you can skip this step if B already has that capability

2. A instructs B where to send cycles
3. B uses either `ic.deposit_cycles` or some other call with attached cycles to transfer as many cycles out as possible
  - You need to keep a few cycles back to pay for actually making that transfer. If you get an error with the message `Call failed to send` you most likely added too many cycles.

4. A can now `ic.delete_canister(<B>)` with minimal cycles loss
