Account creation, storage, resizing, and closure
This page explains the account life cycle shown by the basic Solana examples: create an account, fund the space it needs, write program state, increase or reduce its space, and close it when its owner no longer needs it. The examples also show how a program-derived address (PDA) can hold rent funds and how a program can return account funds without allowing one user to close another user's account.
See the Solana programming basics page for the wider account model. For related transaction patterns, see instruction and CPI patterns and program-derived address design.
Account ownership
An account has an owner program that controls changes to its data. The account-creation examples contrast a system-owned account that mainly holds SOL with a program-owned account that stores data for an application.
| Account use | Lifecycle purpose |
|---|---|
| System account | Holds SOL or acts as a destination for a transfer. The System Program owns it. |
| Program state account | Holds serialized application data. The application program owns it after creation. |
| PDA state account | Gives a program a stable address derived from seeds. The program can validate and sign for that address. |
The account owner matters at every later stage. The program that owns an account can update its data, resize it, or close it when the instruction also checks the correct authority.
Account creation and initialization
Account creation combines a payer, an address, an owner, a data size, and enough lamports for that size. After the account exists, initialization writes the first valid value that follows the program's data format.
System account creation
The basics/create-account example creates a system-owned account in two ways. A client can call the System Program directly, or it can send the request to an application program that calls the System Program through a cross-program invocation (CPI). Both paths produce an account owned by the System Program and are useful when the account does not hold application state.
The example derives the required account size from the requested data and uses the Rent sysvar to find the minimum lamport balance for that size. This keeps the initial funding tied to the space that the new account needs.
References: basics/…/lib.rs• L10-32 basics/…/test_create_account.rs• L15-45 basics/…/create-account-asm-program.s• L28-177 basics/…/test.ts• L17-85 basics/…/lib.rs• L11-41
Program state initialization
The account-data examples create an address record owned by the application program. The record contains a name, house number, street, and city. The program first allocates space for the serialized record and then writes the supplied value into the new account.
The Anchor example places size limits on text fields before it calculates the allocation. The native example measures the serialized input. In both cases, the stored data must fit the account space selected during creation.
References: basics/…/create.rs• L4-26 basics/…/address_info.rs• L3-12 basics/…/create.rs• L13-34 basics/…/address_info.rs• L3-14
Account space and rent
Account space is the byte capacity reserved for account data. Rent funding is linked to that capacity, so a change in the data shape can also change the lamport balance that the account must hold.
When a program creates or grows an account, it calculates the minimum balance for the target size through the Rent sysvar. The payer supplies any additional lamports needed for the new size. This relationship is the key contract between the data format and the account balance.
Account resizing
Resizing changes the capacity of an existing account so its stored value can grow or shrink. The basics/realloc examples initialize a message or address record, then update it with a new size based on the replacement value.
Growth and shrinkage
An update can require more space than the current account has, or less space than it currently uses. The program computes the new size before it changes the account. When the new size needs more rent, a payer adds the difference; when the size is smaller, the account no longer needs the old capacity.
The examples separate two concerns: account resizing changes the storage capacity, while serialization or direct data writes give the resized bytes meaning. A successful resize without a matching data update does not by itself create a valid new state value.
References: basics/…/lib.rs• L9-61 basics/…/test_realloc.rs• L37-70 basics/…/create.rs• L12-33 basics/…/reallocate.rs• L11-34 basics/…/processor.rs• L6-19
Zero initialization
Reallocation can use a mode that clears newly added bytes, or a mode that leaves them for the program to fill. The basics/realloc examples show both choices: one grows an address record and writes the added fields, while the other grows a work record and writes the complete value into the resized region.
The choice affects the safety of later reads. Code that does not clear new space must write every byte that later logic treats as state before that logic reads the account.
References: basics/…/reallocate.rs• L11-47 basics/…/reallocate.rs• L11-43 basics/…/lib.rs• L35-48 basics/…/test_realloc.rs• L47-70
PDA-funded account creation
A PDA is an address derived from a program and a seed set rather than from a private key. The basics/pda-rent-payer example uses a PDA as a rent vault: the program keeps lamports at the known address and uses those funds to create or fund another account.
The flow has two stages:
- The program creates or funds the rent-vault PDA with a chosen reserve.
- The program checks the same PDA seeds and transfers the minimum balance for the new account from the vault.
The PDA does not need a private key. When the System Program must treat the PDA as the authority for a CPI, the application supplies the PDA's seeds and bump so the runtime can verify the program's signature.
The rent-payer example does not restrict the caller of the account-creation instruction. Any caller can therefore use the vault's lamports to fund an account of their choice. A production flow must add an authority check before allowing the PDA to spend its reserve.
This diagram shows how a program-controlled reserve funds a separate account.
flowchart LR
Payer -->|funds| Vault[Rent-vault PDA]
Program[Application program] -->|validates seeds| Vault
Vault -->|pays minimum balance| New[New account]
System[System Program] -->|creates or receives funds| New
References: basics/…/init_rent_vault.rs• L4-30 basics/…/create_new_account.rs• L4-41 basics/…/test_pda_rent_payer.rs• L30-76 basics/…/init_rent_vault.rs• L17-43 basics/…/create_new_account.rs• L10-27 basics/…/processor.rs• L9-20
Safe account closure
Closing an account ends its data life cycle and returns its remaining lamports to an approved recipient. The basics/close-account example creates a user record at a PDA derived from the USER seed and the user's public key, then permits that user to close only that PDA.
Authority checks
The close instruction requires the user to sign and derives the expected PDA from the signing user's key. It rejects an account at a different address before moving funds. This check prevents a valid user from using the close path against another user's state account.
References: basics/…/close_user.rs• L4-23 basics/…/test_close_account.rs• L20-53 basics/…/close_user.rs• L9-25 basics/…/create_user.rs• L12-36 basics/…/processor.rs• L7-18
Fund recovery and reset
After authority checks pass, the close flow returns the account's available lamports to the user, reduces its data size to zero, and assigns it to the System Program. The account no longer carries the old program state after these steps.
Anchor expresses the same lifecycle with a close constraint on the account. The native and low-level examples show the state transitions directly, which makes the required order clear: validate authority, recover funds, remove data, and release ownership.
References: basics/…/close_user.rs• L9-22 basics/…/test_close_account.rs• L46-53 basics/…/close_user.rs• L27-42 basics/…/create_user.rs• L18-35
Change checklist
When these files or sections are changed, remember to consider these:
- Update every size calculation when a stored field or its maximum length changes. Creation, resizing, serialization, and rent funding must use the same data shape.
- Keep PDA seed prefixes and bump handling aligned between account creation and closure. A seed change can make existing accounts unreachable.
- Keep the account owner and the System Program account role aligned across direct calls and CPI calls. A mismatch can make creation, resizing, or closure fail.
- Preserve the close authority check when the account address or user relationship changes. The recipient of recovered lamports must remain the authorized owner.
- Keep the zero-initialization choice aligned with the code that reads newly allocated bytes.
- Keep an authority check in front of any instruction that spends lamports held by a PDA. PDA signing proves program control, not caller permission.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Stored data is larger than the allocated space | Initialization or update cannot write a valid complete value | Derive space from the same schema and field limits used for serialization |
| A larger account is funded below its new minimum balance | Reallocation cannot meet the account's rent requirement | Recompute the minimum balance after every size change and fund the difference |
| A close request names another user's PDA | The instruction must reject the request rather than return that account's funds | Require the signer and derive the expected PDA from that signer |
| New bytes are read after a non-zero-initialized resize | The program can read data that was never set as valid state | Write all newly used fields before later reads, or use zero initialization |
| PDA seeds or bump do not match the vault or state account | PDA validation or a signed CPI fails | Use one seed contract for derivation, account checks, and PDA signing |
| An unrestricted caller invokes the rent-vault funding instruction | The caller can spend the vault reserve on an arbitrary account | Check an authorized signer before the PDA signs the funding CPI |
Key files
| File | Why to read it |
|---|---|
| basics/…/README.md | Gives the high-level contrast between direct System Program account creation and creation through a program. |
| basics/…/lib.rs | Shows how account size and rent funding enter a System Program creation request. |
| basics/…/create.rs | Shows the program-state creation contract and its field-size limits. |
| basics/…/lib.rs | Shows the user-facing initialize and update lifecycle for a variable-size value. |
| basics/…/reallocate.rs | Shows the separate concerns of rent adjustment, resizing, and writing resized data. |
| basics/…/create_new_account.rs | Shows how a PDA-backed reserve funds a new account through the System Program. |
| basics/…/init_rent_vault.rs | Shows how the program establishes and funds the rent-vault PDA. |
| basics/…/close_user.rs | Shows the compact authority and close contract for a user PDA. |
| basics/…/close_user.rs | Makes the fund recovery, data reset, and ownership release steps explicit. |