accountUnsubscribe
Unsubscribe from account change notifications
Common use cases
| React Component Cleanup | Call accountUnsubscribe in useEffect cleanup function or componentWillUnmount to prevent memory leaks when components unmount. Ensures server resources are freed and prevents stale event handlers from firing after component destroyed. |
| Route Change / Navigation | Unsubscribe from account subscriptions when user navigates to different page or switches wallet accounts. Prevents accumulation of obsolete subscriptions and reduces unnecessary server-to-client bandwidth. |
| Connection Reset / Reconnection | Cancel all active subscriptions before closing WebSocket connection or during reconnection logic. Required for clean connection lifecycle management and prevents orphaned subscriptions on server. |
Parameters
subscriptionId (number, required)
id of the account Subscription to cancel
Request
- wscat
- JavaScript
- Python
wscat -c wss://solana-mainnet.api.syndica.io -x '{
"jsonrpc": "2.0",
"id": 1,
"method": "accountUnsubscribe",
"params": [
0
]
}'
const ws = new WebSocket('wss://solana-mainnet.api.syndica.io');
ws.on('open', function open() {
ws.send(JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "accountUnsubscribe",
"params": [
0
]
}));
});
ws.on('message', function incoming(data) {
console.log(data);
});
import websocket
import json
ws = websocket.create_connection('wss://solana-mainnet.api.syndica.io')
ws.send(json.dumps({
"jsonrpc": "2.0",
"id": 1,
"method": "accountUnsubscribe",
"params": [
0
]
}))
result = ws.recv()
print(result)
ws.close()
Replace mainnet with devnet in the URL to query devnet instead.
Response
{
"jsonrpc": "2.0",
"result": true,
"id": 1
}
Response Fields
Return value: boolean
unsubscribe success message
FAQ and Troubleshooting
What happens if I call accountUnsubscribe with an invalid subscription ID?
Server returns JSON-RPC error with code -32602 and message "Invalid subscription id." This occurs when subscription ID doesn't exist, was already unsubscribed, or connection lost subscription state. Always store subscription IDs and handle error gracefully.
Do I need to unsubscribe before closing the WebSocket connection?
While WebSocket closure automatically cleans up subscriptions server-side, explicit accountUnsubscribe is best practice. It immediately frees resources, prevents race conditions, and ensures deterministic cleanup, especially for connection pools or reconnection scenarios.
Can I unsubscribe from a subscription created on a different WebSocket connection?
No. Subscription IDs are scoped to the WebSocket connection that created them. Each connection maintains its own subscription namespace. Attempting to unsubscribe a subscription ID from a different connection returns "Invalid subscription id" error.
Related Methods
accountSubscribe (WebSocket)
Creates account subscription that accountUnsubscribe cancels. Returns subscription ID required for unsubscribe. Every accountSubscribe should have corresponding accountUnsubscribe for proper resource management.
programUnsubscribe (WebSocket)
Cancels programSubscribe subscriptions. Same unsubscribe pattern: single subscription ID parameter, returns boolean. Use same cleanup practices as accountUnsubscribe.
signatureUnsubscribe (WebSocket)
Cancels signatureSubscribe subscriptions. Similar error handling: invalid ID returns -32602. Signature subscriptions auto-expire after confirmation, but explicit unsubscribe recommended.
slotUnsubscribe (WebSocket)
Cancels slotSubscribe subscriptions. Follows identical unsubscribe pattern. Critical for applications monitoring many slots to prevent subscription accumulation.
External Resources
- Solana Official Documentation
- Anza agave: PubSub Unsubscribe Implementation - Official validator source showing server-side unsubscribe logic and subscription cleanup. Includes test coverage demonstrating proper error handling for invalid subscription IDs.