Close Token Account
The closeTokenAccount function closes a token account and reclaims the rent. The token account must have a zero balance before it can be closed.
Usage
// Close using token account address directly
const signature = await connection.closeTokenAccount({
owner, // owner of the token account
tokenAccount, // address of the token account to close
});
// Or close using wallet and mint addresses
const signature = await connection.closeTokenAccount({
owner, // owner of the token account
wallet, // wallet address
mint, // token mint address
});Parameters
owner:KeyPairSigner- Owner of the token account (must sign)tokenAccount:Address(optional) - Direct token account address to closewallet:Address(optional) - Wallet address (required if tokenAccount not provided)mint:Address(optional) - Token mint address (required if tokenAccount not provided)destination:Address(optional) - Where to send reclaimed rent (defaults to owner’s address)useTokenExtensions:boolean(optional) - Use Token Extensions (Token-2022) program instead of classic Token program (default: true)skipPreflight:boolean(optional) - Skip pre-flight transaction checks (default: true)maximumClientSideRetries:number(optional) - Number of retry attempts if the transaction fails (default: 0)abortSignal:AbortSignal | null(optional) - Signal to cancel the transaction (default: null)
Returns
Returns a Promise<string> - The transaction signature that can be used to look up the transaction.
Example
import { lamports, SOL } from "@solana/kit";
// Create a wallet and mint some tokens
const owner = await connection.createWallet({
airdropAmount: lamports(1n * SOL),
});
const mintAddress = await connection.createTokenMint({
mintAuthority: owner,
decimals: 9,
name: "My Token",
symbol: "TKN",
uri: "https://example.com/token.json",
});
// Mint tokens to create the token account
await connection.mintTokens(mintAddress, owner, 100n, owner.address);
// Burn all tokens to bring balance to zero
await connection.burnTokens({
mintAddress,
owner,
amount: 100n,
});
// Close the token account and reclaim rent
const signature = await connection.closeTokenAccount({
owner,
wallet: owner.address,
mint: mintAddress,
});
console.log(`Token account closed: ${signature}`);
// Optionally, specify where to send the reclaimed rent
const recipient = "RecipientAddressHere...";
await connection.closeTokenAccount({
owner,
wallet: owner.address,
mint: mintAddress,
destination: recipient as Address,
});Error Handling
The function will throw an error if:
- The token account has a non-zero balance
- The owner doesn’t have permission to close the account
- Neither tokenAccount nor both wallet and mint are provided
- The token account doesn’t exist
- The owner lacks sufficient SOL to pay for the transaction
- The RPC connection fails
Notes
- The token account must have a zero balance before closing
- Closing a token account reclaims the rent (approximately 0.00203928 SOL)
- If no destination is specified, rent is returned to the owner’s address
- Supports both Token Extensions (Token-2022) and classic Token programs
- You must either provide the
tokenAccountaddress directly, or bothwalletandmintto derive it
Last updated on