# Failed deleting canister on mainnet

**URL:** <https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705>\
**Category:** Rust\
**Created:** [November 24, 2024, 12:21pm UTC](https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705 "2024-11-24T12:21:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumanaa](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jumanaa](https://forum.dfinity.org/u/jumanaa)\
**Post date:** [November 24, 2024, 12:21pm UTC](https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705/1 "2024-11-24T12:21:29Z")

</div>

Hello, when I tried to delete this canister on mainnet it always gives me this error, how can I delete canister on mainnet and what this error means?

Command: dfx canister delete icrc1\_ledger\_canister --network=ic

Error:  
Beginning withdrawal of cycles; on failure try --no-wallet --no-withdrawal.  
WARNING!  
Canister icrc1\_ledger\_canister has not been stopped. Delete anyway?  
Do you want to proceed? yes/No  
y  
Setting the controller to identity principal.  
Installing temporary wallet in canister icrc1\_ledger\_canister to enable transfer of cycles.  
Attempting to transfer 2191853153265 cycles to canister zhdv6-haaaa-aaaan-qzmsq-cai.  
Error: Failed to delete canister ‘icrc1\_ledger\_canister’.  
Caused by: Failed to delete canister ‘icrc1\_ledger\_canister’.  
Failed to deposit 2191853153265 cycles into zhdv6-haaaa-aaaan-qzmsq-cai.  
Failed to call update function ‘deposit\_cycles’ regarding canister ‘zhdv6-haaaa-aaaan-qzmsq-cai’.  
Update call using wallet failed.  
The replica returned a rejection error: reject code CanisterError, reject message Error from Canister 7p6gu-biaaa-aaaap-aknta-cai: Canister 7p6gu-biaaa-aaaap-aknta-cai is out of cycles, error code None

---

<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 25, 2024, 9:28am UTC](https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705/2 "2024-11-25T09:28:08Z")

</div>

It means that dfx is trying to recover too many cycles. I’ve tried a few times to make this work automatically. It’s been painful…

To make this work, use `--initial-margin <cycles amount>` with increasing numbers until it works. I would start with `--initial-margin 100b` and increase by 50b until it works

---

<div class="post-metadata">

**Author:** ![jumanaa](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jumanaa](https://forum.dfinity.org/u/jumanaa)\
**Post date:** [November 25, 2024, 12:40pm UTC](https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705/3 "2024-11-25T12:40:25Z")

</div>

Do you mean I should add cycles to my 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 25, 2024, 1:15pm UTC](https://forum.dfinity.org/t/failed-deleting-canister-on-mainnet/37705/4 "2024-11-25T13:15:01Z")

</div>

No, you should (manually) leave more cycles in the canister when deleting. Not all cycles can be extracted because of how the protocol works. I’ve unsuccessfully tried to minimize that as much as possible for way too much time and have found no better solution yet.

With `--initial-margin` you specify how many cycles should be left in the canister before deleting it. If the margin is large enough it will succeed in withdrawing excess cycles. If the margin is too large you will unnecessarily delete cycles.
