# Close Position

## 1. Close Position

You can directly close position through the close position method in the Cetus DLMM pool module.

When closing a position, if the position is not empty and still contains liquidity, fees, or rewards, an error code 0 will be thrown by the position module. To successfully close the position, follow these steps:

1. [Remove liquidity](/cetus-developer-docs/developer/via-dlmm-contract/features-available/remove-liquidity.md)(if existed)[.](/cetus-developer-docs/developer/via-dlmm-contract/features-available/remove-liquidity.md)
2. [Collect fees](/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-fee.md)(if existed)[.](/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-fee.md)
3. [Collect rewards](/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-reward.md) (if existed).
4. Close the position.
5. Destroy the `ClosePositionCert` returned by the close position function.

```move
/// Closes a position and returns all underlying tokens and fees.
///
/// This function completely closes a position, removing all liquidity
/// and returning the underlying tokens plus accumulated fees.
///
/// ## Type Parameters
/// - `CoinTypeA`: First token type in the pool
/// - `CoinTypeB`: Second token type in the pool
///
/// ## Parameters
/// - `pool`: Mutable reference to the pool
/// - `position`: Position to close (consumed)
/// - `config`: Global configuration
/// - `versioned`: Versioned object for compatibility check
/// - `clk`: Clock for timestamp tracking
/// - `ctx`: Transaction context
///
/// ## Returns
/// - `(ClosePositionCert, Balance<CoinTypeA>, Balance<CoinTypeB>)`: Certificate and token balances
///
/// ## Events Emitted
/// - `ClosePositionEvent`: Contains position and closure details
///
/// ## Errors
/// - `EPositionPoolNotMatch`: If position doesn't belong to this pool
public fun close_position<CoinTypeA, CoinTypeB>(
    pool: &mut Pool<CoinTypeA, CoinTypeB>,
    position: Position,
    config: &GlobalConfig,
    versioned: &Versioned,
    clk: &Clock,
    ctx: &TxContext,
): (ClosePositionCert, Balance<CoinTypeA>, Balance<CoinTypeB>) {
    ...
}
```

## 2. Close Position With Fee

Closes a position and returns a `ClosePositionCert` along with all underlying tokens and accumulated fees.

```move
/// This function completely closes a position, removing all liquidity
/// and returning the underlying tokens plus accumulated fees.
///
/// ## Type Parameters
/// - `CoinTypeA`: First token type in the pool
/// - `CoinTypeB`: Second token type in the pool
///
/// ## Parameters
/// - `pool`: Mutable reference to the pool
/// - `position`: Position to close (consumed)
/// - `config`: Global configuration
/// - `versioned`: Versioned object for compatibility check
/// - `clk`: Clock for timestamp tracking
/// - `ctx`: Transaction context
///
/// ## Returns
/// - `(ClosePositionCert, Balance<CoinTypeA>, Balance<CoinTypeB>, Balance<CoinTypeA>, Balance<CoinTypeB>)`: Certificate and token balances
///
/// ## Events Emitted
/// - `ClosePositionEvent`: Contains position and closure details
public fun close_position_with_fee<CoinTypeA, CoinTypeB>(
    pool: &mut Pool<CoinTypeA, CoinTypeB>,
    position: Position,
    config: &GlobalConfig,
    versioned: &Versioned,
    clk: &Clock,
    ctx: &TxContext,
): (
    ClosePositionCert,
    Balance<CoinTypeA>,
    Balance<CoinTypeB>,
    Balance<CoinTypeA>,
    Balance<CoinTypeB>,
){
    ...
}
```

### 3. Destroy Close Position Certificate

After closing a position, you must destroy the `ClosePositionCert` to complete the process. This certificate serves as a hot potato pattern to ensure proper cleanup of position resources.

```move
/// Destroys a close position certificate.
///
/// This function destroys a close position certificate after all rewards
/// have been extracted from it.
///
/// ## Parameters
/// - `cert`: Close position certificate to destroy (consumed)
/// - `versioned`: Versioned object for compatibility check
public fun destroy_close_position_cert(
    cert: ClosePositionCert, 
    versioned: &Versioned
) {
    ...
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/close-position.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
