> ## Documentation Index
> Fetch the complete documentation index at: https://lightprotocol-migration.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Burn Compressed Accounts

> Guide to burn compressed accounts in Solana programs with full code examples.

Compressed accounts are permanently burned via CPI to the Light System Program.

Burning a compressed account

* consumes the existing account hash, and
* produces no output state.
* A burned account cannot be reinitialized.

<Check>
  Find [full code examples at the end](/compressed-pdas/guides/how-to-burn-compressed-accounts#full-code-example) for Anchor and native Rust.
</Check>

## Implementation Guide

This guide will cover the components of a Solana program that burns compressed accounts.\
Here is the complete flow to burn compressed accounts:

<div className="hidden dark:block">
  <Frame>
    <img src="https://mintcdn.com/lightprotocol-migration/ScTcwQmaQFSwGvd2/images/program-burn-1.png?fit=max&auto=format&n=ScTcwQmaQFSwGvd2&q=85&s=cf4aac9a76e42178b80459aea504532a" alt="" width="1146" height="639" data-path="images/program-burn-1.png" />
  </Frame>
</div>

<div className="block dark:hidden">
  <Frame>
    <img src="https://mintcdn.com/lightprotocol-migration/ScTcwQmaQFSwGvd2/images/program-burn.png?fit=max&auto=format&n=ScTcwQmaQFSwGvd2&q=85&s=47ace4830776e8590cdc4d526b7c6fc2" alt="" width="1146" height="639" data-path="images/program-burn.png" />
  </Frame>
</div>

<Steps>
  <Step title="Program Setup">
    <Accordion title="Dependencies, Constants, Compressed Account">
      **Dependencies**

      Add dependencies to your program.

      ```toml theme={null}
      [dependencies]
      light-sdk = "0.16.0"
      anchor_lang = "0.31.1"
      ```

      ```toml theme={null}
      [dependencies]
      light-sdk = "0.16.0"
      borsh = "0.10.0"
      solana-program = "2.2"
      ```

      * The `light-sdk` provides macros, wrappers and CPI interface to create and interact with compressed accounts.
      * Add the serialization library (`borsh` for native Rust, or use `AnchorSerialize`).

      **Constants**

      Set program address and derive the CPI authority PDA to call the Light System program.

      ```rust theme={null}
      declare_id!("rent4o4eAiMbxpkAM1HeXzks9YeGuz18SEgXEizVvPq");

      pub const LIGHT_CPI_SIGNER: CpiSigner =
          derive_light_cpi_signer!("rent4o4eAiMbxpkAM1HeXzks9YeGuz18SEgXEizVvPq");
      ```

      **`CPISigner`** is the configuration struct for CPI's to the Light System Program.

      * CPIs to the Light System program must be signed with a PDA derived by your program with the seed `b"authority"`
      * `derive_light_cpi_signer!` derives the CPI signer PDA for you at compile time.

      **Compressed Account**

      Define your compressed account struct.

      \#\[event] // declared as event so that it is part of the idl.#\[derive( Clone, Debug, Default, LightDiscriminator)]pub struct MyCompressedAccount \{ pub owner: Pubkey, pub message: String,}#\[derive( Debug, Default, Clone, BorshSerialize, BorshDeserialize, LightDiscriminator,)]pub struct MyCompressedAccount \{ pub owner: Pubkey, pub message: String,}

      You derive

      * the standard traits (`Clone`, `Debug`, `Default`),
      * `borsh` or `AnchorSerialize` to serialize account data, and
      * `LightDiscriminator` to implements a unique type ID (8 bytes) to distinguish account types. The default compressed account layout enforces a discriminator in its *own field*, [not the first 8 bytes of the data field](#user-content-fn-1)\[^1].

      <Info>
        The traits listed above are required for `LightAccount`. `LightAccount` wraps `MyCompressedAccount` in Step 3 to set the discriminator and create the compressed account's data.
      </Info>
    </Accordion>
  </Step>

  <Step title="Instruction Data">
    Define the instruction data with the following parameters:

    <Tabs>
      <Tab title="Anchor">
        ```rust theme={null}
        pub fn burn_account<'info>(
            ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
            proof: ValidityProof,
            account_meta: CompressedAccountMetaBurn,
            current_message: String,
        ) -> Result<()>
        ```
      </Tab>

      <Tab title="Native Rust">
        ```rust theme={null}
        pub struct BurnInstructionData {
            pub proof: ValidityProof,
            pub account_meta: CompressedAccountMetaBurn,
            pub current_account: MyCompressedAccount,
        }
        ```
      </Tab>
    </Tabs>

    1. **Validity Proof**

    * Define `proof` to include the proof that the account exists in the state tree.
    * Clients fetch a validity proof with `getValidityProof()` from an RPC provider that supports ZK Compression (Helius, Triton, ...).

    2. **Specify input state**

    * Define `account_meta: CompressedAccountMetaBurn` to reference the existing account for the Light System Program to nullify permanently:
      * `tree_info: PackedStateTreeInfo`: References the existing account hash in the state tree.
      * `address`: The account's derived address.

    <Info>
      Burn does not specify an output state tree. `CompressedAccountMetaBurn` omits `output_state_tree_index` because no output state is created.
    </Info>

    3. **Current account data**

    * Define fields to include the current account data passed by the client.
    * This depends on your program logic. This example includes `current_message` (or `current_account` in Native Rust).
  </Step>

  <Step title="Burn Compressed Account">
    Burn the compressed account permanently with `LightAccount::new_burn()`. No account can be reinitialized at this address in the future.

    <Check>
      `new_burn()`

      1. hashes the current account data as input state and
      2. creates no output state to burn the account permanently.
    </Check>

    <Tabs>
      <Tab title="Anchor">
        ```rust theme={null}
        let my_compressed_account = LightAccount::<MyCompressedAccount>::new_burn(
            &crate::ID,
            &account_meta,
            MyCompressedAccount {
                owner: ctx.accounts.signer.key(),
                message: current_message,
            },
        )?;
        ```
      </Tab>

      <Tab title="Native Rust">
        ```rust theme={null}
        let my_compressed_account = LightAccount::<MyCompressedAccount>::new_burn(
            &ID,
            &instruction_data.account_meta,
            instruction_data.current_account,
        )?;
        ```
      </Tab>
    </Tabs>

    **Pass these parameters to `new_burn()`:**

    * `&program_id`: The program's ID that owns the compressed account.
    * `&account_meta`: The `CompressedAccountMetaBurn` from instruction data (*Step 2*) that identifies the existing account for the Light System Program to nullify permanently.
      * Anchor: Pass `&account_meta` directly
      * Native Rust: Pass `&instruction_data.account_meta`
    * Include the curent account data.
      * Anchor: Build `MyCompressedAccount` with `owner` and `message`.
      * Native Rust: Pass `instruction_data.current_account` directly.

    **The SDK creates:**

    * A `LightAccount` wrapper that marks the account as permanently burned with no output state.

    <Info>
      `new_burn()` hashes the input state. The Light System Program verifies the input hash and nullifies it in *Step 4*.
    </Info>
  </Step>

  <Step title="Light System Program CPI">
    The Light System Program CPI burns the compressed account permanently.

    <Check>
      The Light System Program

      * validates the account exists in state tree with the validity,
      * nullifies the existing account hash, and
      * creates no output state.
    </Check>

    <Tabs>
      <Tab title="Anchor">
        ```rust theme={null}
        let light_cpi_accounts = CpiAccounts::new(
            ctx.accounts.signer.as_ref(),
            ctx.remaining_accounts,
            crate::LIGHT_CPI_SIGNER,
        );

        LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, proof)
            .with_light_account(my_compressed_account)?
            .invoke(light_cpi_accounts)?;
        ```

        **Set up `CpiAccounts::new()`:**

        `CpiAccounts::new()` parses accounts for the CPI call to Light System Program.

        **Pass these parameters:**

        * `ctx.accounts.signer.as_ref()`: the transaction signer
        * `ctx.remaining_accounts`: Slice with `[system_accounts, ...packed_tree_accounts]`. The client builds this with `PackedAccounts` and passes it to the instruction.
        * `&LIGHT_CPI_SIGNER`: Your program's CPI signer PDA defined in Constants.
      </Tab>

      <Tab title="Native Rust">
        ```rust theme={null}
        let (signer, remaining_accounts) = accounts
            .split_first();

        let cpi_accounts = CpiAccounts::new(
            signer,
            remaining_accounts,
            LIGHT_CPI_SIGNER
        );

        LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, instruction_data.proof)
            .with_light_account(my_compressed_account)?
            .invoke(cpi_accounts)?;
        ```

        **Set up `CpiAccounts::new()`:**

        `CpiAccounts::new()` parses accounts for the CPI call to Light System Program.

        **Pass these parameters:**

        * `signer`: account that signs and pays for the transaction
        * `remaining_accounts`: Slice with `[system_accounts, ...packed_tree_accounts]`. The client builds this with `PackedAccounts`.
          * `split_first()` extracts the fee payer from the accounts array to separate it from the Light System Program accounts needed for the CPI.
        * `&LIGHT_CPI_SIGNER`: Your program's CPI signer PDA defined in Constants.
      </Tab>
    </Tabs>

    <Accordion title="System Accounts List">
      |    | Name                                                                                                                                                                                                                                                                     | Description                                                                                                                                                                                   |
      | :- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | 1  | <Tooltip cta="Program ID" href="https://solscan.io/account/SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7" tip="SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7">Light System Program</Tooltip>                                                                                 | Verifies validity proofs, compressed account ownership checks, CPIs the account compression program to update tree accounts                                                                   |
      | 2  | CPI Signer                                                                                                                                                                                                                                                               | - PDA to sign CPI calls from your program to Light System Program<br />- Verified by Light System Program during CPI<br />- Derived from your program ID                                      |
      | 3  | Registered Program PDA                                                                                                                                                                                                                                                   | - Access control to the Account Compression Program                                                                                                                                           |
      | 4  | <Tooltip cta="Program ID" href="https://solscan.io/account/noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV" tip="noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV">Noop Program</Tooltip>                                                                                         | - Logs compressed account state to Solana ledger. Only used in v1.<br />- Indexers parse transaction logs to reconstruct compressed account state                                             |
      | 5  | <Tooltip cta="Program ID" tip="PDA derived from Light System Program ID with seed b 'cpi_authority' HZH7qSLcpAeDqCopVU4e5XkhT9j3JFsQiq8CmruY3aru" href="https://solscan.io/account/HZH7qSLcpAeDqCopVU4e5XkhT9j3JFsQiq8CmruY3aru">Account Compression Authority</Tooltip> | Signs CPI calls from Light System Program to Account Compression Program                                                                                                                      |
      | 6  | <Tooltip cta="Program ID" tip="compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq" href="https://solscan.io/account/compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq">Account Compression Program</Tooltip>                                                                          | - Writes to state and address tree accounts<br />- Client and the account compression program do not interact directly.                                                                       |
      | 7  | Invoking Program                                                                                                                                                                                                                                                         | Your program's ID, used by Light System Program to:<br />- Derive the CPI Signer PDA<br />- Verify the CPI Signer matches your program ID<br />- Set the owner of created compressed accounts |
      | 8  | <Tooltip tip="11111111111111111111111111111111" cta="Program ID" href="https://solscan.io/account/11111111111111111111111111111111">System Program</Tooltip>                                                                                                             | Solana System Program to transfer lamports                                                                                                                                                    |
    </Accordion>

    **Build the CPI instruction**:

    * `new_cpi()` initializes the CPI instruction with the `proof` to prove the account exists in the state tree *- defined in the Instruction Data (Step 2).*
    * `with_light_account` adds the `LightAccount` wrapper configured to burn the account *- defined in Step 3*.
    * `invoke(light_cpi_accounts)` calls the Light System Program with `CpiAccounts`.
  </Step>
</Steps>

## Full Code Example

The example programs below implement all steps from this guide. Make sure you have your [developer environment](https://www.zkcompression.com/compressed-pdas/create-a-program-with-compressed-pdas#start-building) set up first.

```bash theme={null}
npm -g i @lightprotocol/zk-compression-cli@0.27.1-alpha.2
light init testprogram
```

<Warning>
  For help with debugging, see the [Error Cheatsheet](https://www.zkcompression.com/resources/error-cheatsheet).
</Warning>

<Tabs>
  <Tab title="Anchor">
    <Info>
      Find the source code [here](https://github.com/Lightprotocol/program-examples/tree/main/basic-operations/anchor/burn).
    </Info>

    ```rust expandable theme={null}
    #![allow(unexpected_cfgs)]
    #![allow(deprecated)]

    use anchor_lang::{prelude::*, AnchorDeserialize, AnchorSerialize};
    use light_sdk::{
        account::LightAccount,
        address::v1::derive_address,
        cpi::{v1::CpiAccounts, CpiSigner},
        derive_light_cpi_signer,
        instruction::{account_meta::CompressedAccountMetaBurn, PackedAddressTreeInfo, ValidityProof},
        LightDiscriminator,
    };

    declare_id!("BJhPWQnD31mdo6739Mac1gLuSsbbwTmpgjHsW6shf6WA");

    pub const LIGHT_CPI_SIGNER: CpiSigner =
        derive_light_cpi_signer!("BJhPWQnD31mdo6739Mac1gLuSsbbwTmpgjHsW6shf6WA");

    #[program]
    pub mod burn {

        use super::*;
        use light_sdk::cpi::{
            v1::LightSystemProgramCpi, InvokeLightSystemProgram, LightCpiInstruction,
        };

        /// Setup: Creates a compressed account
        pub fn create_account<'info>(
            ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
            proof: ValidityProof,
            address_tree_info: PackedAddressTreeInfo,
            output_state_tree_index: u8,
            message: String,
        ) -> Result<()> {
            let light_cpi_accounts = CpiAccounts::new(
                ctx.accounts.signer.as_ref(),
                ctx.remaining_accounts,
                crate::LIGHT_CPI_SIGNER,
            );

            let (address, address_seed) = derive_address(
                &[b"message", ctx.accounts.signer.key().as_ref()],
                &address_tree_info
                    .get_tree_pubkey(&light_cpi_accounts)
                    .map_err(|_| ErrorCode::AccountNotEnoughKeys)?,
                &crate::ID,
            );

            let mut my_compressed_account = LightAccount::<MyCompressedAccount>::new_init(
                &crate::ID,
                Some(address),
                output_state_tree_index,
            );

            my_compressed_account.owner = ctx.accounts.signer.key();
            my_compressed_account.message = message.clone();

            msg!(
                "Created compressed account with message: {}",
                my_compressed_account.message
            );

            LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, proof)
                .with_light_account(my_compressed_account)?
                .with_new_addresses(&[address_tree_info.into_new_address_params_packed(address_seed)])
                .invoke(light_cpi_accounts)?;

            Ok(())
        }

        /// Burns a compressed account permanently
        pub fn burn_account<'info>(
            ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
            proof: ValidityProof,
            account_meta: CompressedAccountMetaBurn,
            current_message: String,
        ) -> Result<()> {
            let light_cpi_accounts = CpiAccounts::new(
                ctx.accounts.signer.as_ref(),
                ctx.remaining_accounts,
                crate::LIGHT_CPI_SIGNER,
            );

            let my_compressed_account = LightAccount::<MyCompressedAccount>::new_burn(
                &crate::ID,
                &account_meta,
                MyCompressedAccount {
                    owner: ctx.accounts.signer.key(),
                    message: current_message,
                },
            )?;

            msg!("Burning compressed account permanently");

            LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, proof)
                .with_light_account(my_compressed_account)?
                .invoke(light_cpi_accounts)?;

            Ok(())
        }
    }

    #[derive(Accounts)]
    pub struct GenericAnchorAccounts<'info> {
        #[account(mut)]
        pub signer: Signer<'info>,
    }

    #[event]
    #[derive(Clone, Debug, Default, LightDiscriminator)]
    pub struct MyCompressedAccount {
        pub owner: Pubkey,
        pub message: String,
    }
    ```
  </Tab>

  <Tab title="Native Rust">
    <Info>
      Find the source code [here](https://github.com/Lightprotocol/program-examples/tree/main/basic-operations/native/programs/burn).
    </Info>

    ```rust expandable theme={null}
    #![allow(unexpected_cfgs)]

    #[cfg(any(test, feature = "test-helpers"))]
    pub mod test_helpers;

    use borsh::{BorshDeserialize, BorshSerialize};
    use light_macros::pubkey;
    use light_sdk::{
        account::sha::LightAccount,
        address::v1::derive_address,
        cpi::{
            v1::{CpiAccounts, LightSystemProgramCpi},
            CpiSigner, InvokeLightSystemProgram, LightCpiInstruction,
        },
        derive_light_cpi_signer,
        error::LightSdkError,
        instruction::{account_meta::CompressedAccountMetaBurn, PackedAddressTreeInfo, ValidityProof},
        LightDiscriminator,
    };
    use solana_program::{
        account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey,
    };

    pub const ID: Pubkey = pubkey!("CFWrQ8za2yT1xH8yBjYvsDUCWnBH7vXtyVJwqoX5FcNg");
    pub const LIGHT_CPI_SIGNER: CpiSigner = derive_light_cpi_signer!("CFWrQ8za2yT1xH8yBjYvsDUCWnBH7vXtyVJwqoX5FcNg");

    #[cfg(not(feature = "no-entrypoint"))]
    entrypoint!(process_instruction);

    #[derive(Debug, BorshSerialize, BorshDeserialize)]
    pub enum InstructionType {
        Create,
        Burn,
    }

    #[derive(Debug, BorshSerialize, BorshDeserialize)]
    pub struct CreateInstructionData {
        pub proof: ValidityProof,
        pub address_tree_info: PackedAddressTreeInfo,
        pub output_state_tree_index: u8,
        pub message: String,
    }

    #[derive(Debug, BorshSerialize, BorshDeserialize)]
    pub struct BurnInstructionData {
        pub proof: ValidityProof,
        pub account_meta: CompressedAccountMetaBurn,
        pub current_account: MyCompressedAccount,
    }

    #[derive(Debug, Default, Clone, BorshSerialize, BorshDeserialize, LightDiscriminator)]
    pub struct MyCompressedAccount {
        pub owner: Pubkey,
        pub message: String,
    }

    pub fn process_instruction(
        _program_id: &Pubkey,
        accounts: &[AccountInfo],
        instruction_data: &[u8],
    ) -> Result<(), ProgramError> {
        let (instruction_type, rest) = instruction_data
            .split_first()
            .ok_or(ProgramError::InvalidInstructionData)?;

        match InstructionType::try_from_slice(&[*instruction_type])
            .map_err(|_| ProgramError::InvalidInstructionData)?
        {
            InstructionType::Create => create(accounts, rest)?,
            InstructionType::Burn => burn(accounts, rest)?,
        }

        Ok(())
    }

    fn create(accounts: &[AccountInfo], instruction_data: &[u8]) -> Result<(), LightSdkError> {
        let instruction_data =
            CreateInstructionData::try_from_slice(instruction_data).map_err(|_| LightSdkError::Borsh)?;

        let signer = accounts.first().ok_or(ProgramError::NotEnoughAccountKeys)?;

        let light_cpi_accounts = CpiAccounts::new(
            signer,
            &accounts[1..],
            LIGHT_CPI_SIGNER
        );

        let (address, address_seed) = derive_address(
            &[b"message", signer.key.as_ref()],
            &instruction_data
                .address_tree_info
                .get_tree_pubkey(&light_cpi_accounts)
                .map_err(|_| ProgramError::NotEnoughAccountKeys)?,
            &ID,
        );

        let new_address_params = instruction_data
            .address_tree_info
            .into_new_address_params_packed(address_seed);

        let mut my_compressed_account = LightAccount::<MyCompressedAccount>::new_init(
            &ID,
            Some(address),
            instruction_data.output_state_tree_index,
        );
        my_compressed_account.owner = *signer.key;
        my_compressed_account.message = instruction_data.message;

        LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, instruction_data.proof)
            .with_light_account(my_compressed_account)?
            .with_new_addresses(&[new_address_params])
            .invoke(light_cpi_accounts)?;

        Ok(())
    }

    fn burn(accounts: &[AccountInfo], instruction_data: &[u8]) -> Result<(), LightSdkError> {
        let instruction_data =
            BurnInstructionData::try_from_slice(instruction_data).map_err(|_| LightSdkError::Borsh)?;

        let (signer, remaining_accounts) = accounts
            .split_first()
            .ok_or(ProgramError::InvalidAccountData)?;

        let cpi_accounts = CpiAccounts::new(
            signer,
            remaining_accounts,
            LIGHT_CPI_SIGNER
        );

        let my_compressed_account = LightAccount::<MyCompressedAccount>::new_burn(
            &ID,  // Now the burn program owns the account since it created it
            &instruction_data.account_meta,
            instruction_data.current_account,
        )?;

        LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, instruction_data.proof)
            .with_light_account(my_compressed_account)?
            .invoke(cpi_accounts)?;

        Ok(())
    }
    ```
  </Tab>
</Tabs>

## Next Steps

Build a client for your program or get an overview on all compressed account operations.
