- consumes the existing account hash, and
- produces a new account hash with zero values to mark it as closed.
- A closed compressed account can be reinitialized.
Find full code examples at the end for Anchor and native Rust.
Implementation Guide
This guide will cover the components of a Solana program that closes compressed accounts. Here is the complete flow to close compressed accounts:

1
Program Setup
Dependencies, Constants, Compressed Account
Dependencies, Constants, Compressed Account
DependenciesAdd dependencies to your program.
- The
light-sdkprovides macros, wrappers and CPI interface to create and interact with compressed accounts. - Add the serialization library (
borshfor native Rust, or useAnchorSerialize).
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.
- the standard traits (
Clone,Debug,Default), borshorAnchorSerializeto serialize account data, andLightDiscriminatorto 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[^1].
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.2
Instruction Data
Define the instruction data with the following parameters:
- Anchor
- Native Rust
- Validity Proof
- Define
proofto 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, …).
- Specify input state and output state tree (stores closed account hash)
- Define
account_meta: CompressedAccountMetato reference the existing account and specify the state tree to store the new hash with zero values:tree_info: PackedStateTreeInfo: References the existing account hash in the state tree.address: The account’s derived address.output_state_tree_indexpoints to the state tree that will store the updated hash with a zero-byte hash to mark the account as closed.
Clients fetch the current account with
getCompressedAccount() and populate CompressedAccountMeta with the account’s metadata.- Current data
- Define fields to include the current account data passed by the client.
- This depends on your program logic. This example includes the
current_messagefield.
3
Close Compressed Account
Load the compressed account and mark it as closed with Pass these parameters to
LightAccount::new_close().new_close()- hashes the current account data as input state and
- marks the account for closure for the Light System Program.
- Anchor
- Native Rust
new_close():&program_id: The program’s ID that owns the compressed account.&account_meta: TheCompressedAccountMetafrom instruction data (Step 2) that identifies the existing account and specifies the output state tree.- Current account data: The existing account data. The SDK hashes this input state for verification by the Light System Program.
- Anchor: Construct
MyCompressedAccountwithctx.accounts.signer.key()andcurrent_message - Native: Construct
MyCompressedAccountwith data frominstruction_data
- Anchor: Construct
- A
LightAccountwrapper similar to Anchor’sAccountthat marks the account for closure.
new_close() hashes the input state and marks the account for closure. The Light System Program creates output state with zero values:- a zero discriminator (
0u8; 8) removes type identification of the account, - the output contains zeroes as data hash that indicates no data content, and
- the data field contains an empty vector, instead of serialized account fields.
4
Light System Program CPI
Invoke the Light System Program to close the compressed account. This empty account can be reinitialized with Set up
Build the CPI instruction:
LightAccount::new_empty().The Light System Program
- validates the account exists in state tree,
- nullifies the existing account hash, and
- appends the new account hash with zero values to the state tree to mark it as closed.
- Anchor
- Native Rust
CpiAccounts::new():CpiAccounts::new() parses accounts for the CPI call to Light System Program.Pass these parameters:ctx.accounts.signer.as_ref(): the transaction signerctx.remaining_accounts: Slice with[system_accounts, ...packed_tree_accounts]. The client builds this withPackedAccountsand passes it to the instruction.&LIGHT_CPI_SIGNER: Your program’s CPI signer PDA defined in Constants.
System Accounts List
System Accounts List
| Name | Description | |
|---|---|---|
| 1 | 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 - Verified by Light System Program during CPI - Derived from your program ID |
| 3 | Registered Program PDA | - Access control to the Account Compression Program |
| 4 | - Logs compressed account state to Solana ledger. Only used in v1. - Indexers parse transaction logs to reconstruct compressed account state | |
| 5 | Signs CPI calls from Light System Program to Account Compression Program | |
| 6 | - Writes to state and address tree accounts - Client and the account compression program do not interact directly. | |
| 7 | Invoking Program | Your program’s ID, used by Light System Program to: - Derive the CPI Signer PDA - Verify the CPI Signer matches your program ID - Set the owner of created compressed accounts |
| 8 | Solana System Program to transfer lamports |
new_cpi()initializes the CPI instruction with theproofto prove the compressed account exists in the state tree - defined in the Instruction Data (Step 2).with_light_accountadds theLightAccountwrapper configured to close the account with the zero values - defined in Step 3.invoke()calls the Light System Program withCpiAccounts.
Full Code Example
The example programs below implement all steps from this guide. Make sure you have your developer environment set up first, or simply run:- Anchor
- Native Rust
Find the source code for this example here.