# 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](https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/remove-liquidity)(if existed)[.](https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/remove-liquidity)
2. [Collect fees](https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-fee)(if existed)[.](https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-fee)
3. [Collect rewards](https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-dlmm-contract/features-available/collect-reward) (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
) {
    ...
}

```
