> ## Documentation Index
> Fetch the complete documentation index at: https://second.tech/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Handle emergency exits

> Start, progress, and claim emergency exits to move users' bitcoin back on-chain without the Ark server's cooperation.

An unresponsive Ark server leaves your wallet app one route: an [emergency exit](https://second.tech/docs/learn/exit.md). [Respond to forced exits](https://second.tech/docs/patterns/forced-exits.md) covers exits someone else forces.

## Reach for offboarding first

Reserve emergency exits for a server that is unresponsive or uncooperative. Under normal conditions, a cooperative [offboard](https://second.tech/docs/learn/offboard.md) gets bitcoin back on-chain in a single transaction, cheaper and simpler than an exit ([on-chain payments](https://second.tech/docs/patterns/on-chain-payments.md) covers the calls).

## Start the exit

Starting an exit marks the VTXOs for exit. Nothing reaches the chain yet.

> [tab] Bark SDK
>
> * `startExitForEntireWallet()` exits every VTXO in the wallet.
> * `startExitForVtxos(vtxoIds)` exits a chosen set.
> * `cancelExit(vtxoId)` aborts one exit.

> [tab] Bark CLI
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bark exit start --all
> ```
>
> Pass `--vtxo <VTXO_ID>` (repeatable) instead of `--all` to exit a chosen set. `bark exit cancel <VTXO_ID>` aborts one exit.

> [tab] Barkd
>
> * `POST /api/v1/exits/start/all` exits every VTXO in the wallet.
> * `POST /api/v1/exits/start/vtxos` with a list of VTXO IDs exits a chosen set.
> * `POST /api/v1/exits/cancel/{vtxo_id}` aborts one exit.

Five behaviors to account for:

* **Exiting the whole wallet also exits locked VTXOs:** Bark takes every unspent VTXO, including those locked in an in-flight send, Lightning payment, or round.
* **Dust and non-standard VTXOs stay out of an exit:** The whole-wallet call skips them with a warning. The exit can then start with fewer VTXOs than the wallet holds, or none at all. Handle the error that the chosen-set call returns when the set includes one.
* **Cancel works until the final transaction is broadcast:** The VTXO stays spendable after a cancel, even if shared ancestor transactions already reached the chain. Bark refuses the cancel once the exit is past its abortable window or its final transaction reached the mempool or the chain. The SDK's `cancelExit` reports that refusal in its result (`canceled` is `false`, with a `reason`), not as an error. Canceling twice is a no-op.
* **Movements stay pending until the end:** Starting an exit records one movement per VTXO. It stays `pending` until the exit reaches `claimed`, which finishes it as successful, or `vtxo-already-spent` or `canceled`, which finish it as canceled. An exit that ends in `vtxo-swept` leaves its movement `pending`, so read its outcome from the exit state. Track progress through the exit states below, not through the movements.
* **An exit must confirm before the VTXO expires:** After `expiry_height`, the Ark server's sweep can invalidate any exit transaction that has not confirmed yet ([VTXO lifetime](https://second.tech/docs/learn/lifetime.md)). Refresh an expired VTXO instead.

## Fund the fees, then progress until done

Exit transactions pay no fees themselves. Bark fee-bumps them with CPFP children paid from the on-chain wallet, so your app must fund that wallet before the exit can move.

**1. Price the exit with the fee estimate**

Price the VTXOs you are about to exit:

> [tab] Bark SDK
>
> Call `estimateEmergencyExitFee` with the VTXO IDs. Leave the feerate, destination, or fee margin empty (`null`, `nil`, or `undefined`, depending on the language) to use its default. An empty list prices every spendable VTXO. It leaves out the locked VTXOs that a whole-wallet exit includes. The fields below come back as `exitBroadcastFeeSats`, `claimFeeSats`, and `totalFeeSats`. Ignore the deprecated `fundable`. In Rust, `Wallet::estimate_emergency_exit_fee` prices exactly the IDs you pass, so an empty list returns a zero estimate.

> [tab] Bark CLI
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bark exit estimate-fee --all
> ```
>
> Pass `--vtxo <VTXO_ID>` (repeatable) instead of `--all` to price a chosen set.

> [tab] Barkd
>
> Call `GET /api/v1/exits/fee`, with a comma-separated `vtxo_ids` for a chosen set or without it for every spendable VTXO, with the same limit as the SDK.

Read two fields:

* **`exit_broadcast_fee_sat`:** The CPFP fees paid from the confirmed on-chain balance while the exit runs. This is the amount to fund.
* **`claim_fee_sat`:** The fee of the final claim transaction, taken out of the recovered bitcoin. It needs no on-chain balance.

Handle the error that the estimate returns for a dust, non-standard, unknown, spent, or already exited VTXO. A whole-wallet estimate returns that error when one spendable VTXO is dust or non-standard. Present `total_fee_sat` as an estimate. The final cost depends on the feerates when each transaction is broadcast.

> [!TIP]
>
> Check the on-chain balance before the exit starts, not after it stalls. Disable the start action while the confirmed on-chain balance is below `exit_broadcast_fee_sat`.

![Screenshot of the Bark Wallet emergency exit dialog with a confirmed on-chain balance of 0 sats and a disabled Start emergency exit button](https://mintcdn.com/second-0659a37d/tQnT1N_qr7W8s6kQ/images/patterns/exit-dialog-fee-check.png?fit=max\&auto=format\&n=tQnT1N_qr7W8s6kQ\&q=85\&s=85a48ff7c741b71fce87b088e1a161da)

Bark Wallet disables the start button while the confirmed on-chain balance is below the exit broadcast fee

**2. Fund the on-chain wallet**

Send at least `exit_broadcast_fee_sat` to the on-chain wallet, and wait for it to confirm. Get the address from `bark onchain address` on the CLI, from `POST /api/v1/onchain/addresses/next` on Barkd, or from the on-chain wallet your SDK app opened Bark with. Only confirmed bitcoin funds the CPFP children.

**3. Progress the exit**

> [tab] Bark SDK
>
> The background daemon progresses exits while it runs. Call `progressExits` to force a step, or on your own schedule while the daemon is off ([background refresh](https://second.tech/docs/patterns/background-refresh.md#run-one-pass-per-wake) covers mobile wakes). It takes one setting, a feerate. Leave it empty to use Bark's default exit feerate.

> [tab] Bark CLI
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bark exit progress --wait
> ```
>
> The command retries every 60 seconds until `done` turns true, which can take hours or days.

> [tab] Barkd
>
> Barkd progresses registered exits on its own. Call `POST /api/v1/exits/progress` to force a step.

**4. Monitor until done**

The CLI and Barkd progress responses carry three signals:

* **`done`:** True once no exit is left in `start`, `processing`, or `awaiting-delta`.
* **`claimable_height`:** The latest unlock height among the exits whose transactions have all confirmed. It stays `null` until one reaches `awaiting-delta` and ignores exits still in `start` or `processing`, so read it together with `done`.
* **`error`, per exit:** How your app tells a failed run from one that made no headway. When the whole run fails, `bark exit progress` fills a top-level `error` and Barkd answers a 500.

The Bark SDK returns one status per exit instead. Call `allExitsClaimableAtHeight()` for the unlock height.

## Track exit states in the UI

Each exiting VTXO moves through a state machine.

![Diagram of the six exit states from start to claimed and the three states where an exit ends early](https://mintcdn.com/second-0659a37d/tQnT1N_qr7W8s6kQ/images/exit-states.svg?fit=max\&auto=format\&n=tQnT1N_qr7W8s6kQ\&q=85\&s=97b52eb0de89a529db5de81324240b37)

Your wallet app pays CPFP fees while the exit transactions confirm and, until the background daemon claims on its own, makes the claim call once the exit is claimable

> [tab] Bark SDK
>
> `getExitStatus` takes a VTXO ID and two flags, `includeHistory` and `includeTransactions`, and returns that VTXO's state. `listClaimableExits()` returns the VTXOs ready to claim.

> [tab] Bark CLI
>
> `bark exit status <VTXO_ID>` prints one VTXO's state, and `bark exit list` every live exit. Pass `--include-finished` to include exits that reached `claimed`, `vtxo-already-spent`, `vtxo-swept`, or `canceled`.

> [tab] Barkd
>
> `GET /api/v1/exits/status/live` returns every live exit. `/status/finished` returns the exits that reached `claimed`, `vtxo-already-spent`, `vtxo-swept`, or `canceled`. `/status/all` returns both.

Map the states to labels users understand:

| State | Suggested label | What your app does next |
| - | - | - |
| `start` | Exit requested | Waits for the next progress run to broadcast the exit transactions. |
| `processing` | Broadcasting exit transactions | Keeps progressing so every transaction gets fee-bumped and confirmed. |
| `awaiting-delta` | Timelock counting down | Shows the blocks remaining until `claimable_height`. |
| `claimable` | Ready to claim | Prompts for a destination, or claims automatically if that is your exit policy. |
| `claim-in-progress` | Claim pending | Waits for the claim transaction to confirm. Returns to `claimable` if that transaction drops out of the mempool. |
| `claimed` | Back on-chain | Nothing more. The bitcoin is a regular on-chain output. |
| `vtxo-already-spent` | Nothing to exit | Drops the entry. The user spent the VTXO off-chain (a refresh or a send) after the exit started. |
| `vtxo-swept` | Exit failed | Drops the entry and offers a refresh, which can still work depending on what spent the output. An output the exit chain needs was spent on-chain by someone else, usually the server's sweep after `expiry_height`. |
| `canceled` | Exit canceled | Drops the entry. The user canceled the exit before its final transaction was broadcast. The VTXO is still spendable. |

From `awaiting-delta` until the claim completes, the exiting sats show in `pending_exit_sat`. [Balances](https://second.tech/docs/patterns/balances.md#hold-the-last-known-figure-when-an-amount-is-unknown) covers how to display that field.

## Claim to the on-chain wallet

Once a VTXO is `claimable`, claim it to an on-chain address:

> [tab] Bark SDK
>
> Call `drainExits` with `drainAll` set to `true` and an empty `vtxoIds` list to claim every claimable VTXO, or with `vtxoIds` to claim a chosen set. It returns the signed claim transaction as a PSBT. Extract the final transaction with `extractTxFromPsbt` and broadcast it with `broadcastTx`.

> [tab] Bark CLI
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
> bark exit claim <destination> --all
> ```
>
> Pass `--vtxo <VTXO_ID>` instead of `--all` to claim a chosen set.

> [tab] Barkd
>
> `POST /api/v1/exits/claim/all` claims every claimable VTXO, and `POST /api/v1/exits/claim/vtxos` a chosen set.

Until the background daemon claims exits on its own, your app must make this call. After the claim transaction confirms, the bitcoin is ordinary on-chain bitcoin and follows your normal on-chain backup rules.

> [!WARNING]
>
> **Until the claim confirms, the exit depends on the wallet database**
>
> Which exit transactions were broadcast, their CPFP children, and each VTXO's state live only in the local wallet database. The mnemonic does not bring them back. A wallet restored from the mnemonic alone resumes an exit only once that VTXO's own transaction is on-chain or in the mempool. Keep a [continuous backup](https://second.tech/docs/patterns/backups.md#continuous-backups) running through the whole exit, and back up anything your app stores outside that database, such as the claim destination. [Restore a wallet](https://second.tech/docs/patterns/restore.md) lists what each restore path brings back.

## Keep automation and chain access in place

* **Progress in the background:** Once a user triggers an exit, your app progresses it with no further involvement from them. [Automated wallet management](https://second.tech/docs/learn/exit.md#automated-wallet-management) lists what users should never do by hand.
* **Keep the chain source reachable:** An emergency exit never contacts the Ark server. Progressing one needs Esplora or bitcoind to broadcast transactions and watch confirmations.
* **Plan for partial completion:** A [partially completed exit](https://second.tech/docs/learn/exit.md#partially-completed-exits) behaves differently from a finished one. Factor it into any cancellation option you offer.

> [accordion] How Bark Wallet does it
>
> * **Entry points:** "Emergency exit all" in the [Danger zone](https://second.tech/docs/bark-wallet/settings.md#danger-zone) of the Settings page, and "Emergency Exit" for VTXOs selected in the VTXOs table. Both open the same start dialog.
> * **Destination first:** The dialog asks for the destination address before anything is broadcast and stores it per VTXO ID. It warns that the user cannot cancel an exit once it starts, a stricter rule than the library's, and keeps the destination editable until the claim is broadcast.
> * **Cost check:** The dialog shows the total from Bark's exit fee estimate next to the confirmed on-chain balance. It disables the start button while that balance is below the estimate's exit broadcast fee and shows "You don't have enough on-chain bitcoin to cover the exit fees. Deposit more to continue."
> * **Progress and claim:** While an exit is live, the app polls exit status every 10 seconds on every page and claims each `claimable` VTXO to its stored destination. The installed version leaves progress to `barkd`. The web version (WASM) progresses exits from its worker loop. The progress card labels the phases Starting, Broadcasting, Timelocked, Ready to claim, Claiming, and Claimed ([Follow the exit to completion](https://second.tech/docs/bark-wallet/settings.md#follow-the-exit-to-completion)).
> * **After a restore:** The claim destination lives outside the wallet database. Once an exit is claimable, a restored wallet shows "Add address to claim" on the progress card until the user enters one again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.