account_delete
Overview
Section titled “Overview”The account_delete syscall marks an account as deleted by setting the DELETED flag. The account must have zero balance and be writable by the current program.
Syscall Code
Section titled “Syscall Code”Code: 0x06 (TN_SYSCALL_CODE_ACCOUNT_DELETE)
C SDK Function
Section titled “C SDK Function”ulong tsys_account_delete(ulong account_idx);Arguments
Section titled “Arguments”account_idx · ulong · required
Index of the account to delete. Must be writable by the current program and have zero balance.
Return Value
Section titled “Return Value”Returns a syscall result code:
Success · ulong
TN_VM_SYSCALL_SUCCESS(0) - Account marked as deleted successfully
Error Codes · ulong
TN_VM_ERR_SYSCALL_INVALID_ACCOUNT_INDEX(-8) - Account index out of boundsTN_VM_ERR_SYSCALL_ACCOUNT_DOES_NOT_EXIST(-9) - Account does not existTN_VM_ERR_SYSCALL_ACCOUNT_NOT_WRITABLE(-10) - Account not writable by current programTN_VM_ERR_SYSCALL_INVALID_ACCOUNT(-27) - Account has non-zero balance or non-zero nonceTN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOW(-48) - State bytes counter overflow during deactivation
Resource Consumption
Section titled “Resource Consumption”Compute Units
Section titled “Compute Units”- Base cost:
TN_VM_SYSCALL_BASE_COST(512 units) - Additional cost: None
- Total cost: Fixed at 512 units
Memory Pages
Section titled “Memory Pages”- Page usage: No immediate page deallocation (pages freed during garbage collection)
- Metadata impact: May trigger metadata page allocation if not already writable
- Future cleanup: Pages will be reclaimed after transaction completion
State Counter Impact
Section titled “State Counter Impact”- GDSC (Global Deactivated State Counter): Incremented by
TSDK_ACCOUNT_META_FOOTPRINT + account_data_szbytes - Purpose: Tracks deactivated state for the account deletion in the global state counter
- Overflow protection: Returns
TN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOWif counter would overflow
Side Effects
Section titled “Side Effects”- Account state: Sets the DELETED flag on the account
- Metadata: Makes account metadata writable if not already
- Data retention: Account data and metadata remain accessible until garbage collection
Usage Notes
Section titled “Usage Notes”- Account must have exactly zero balance before deletion
- Only the account owner (program) can delete the account
- Deleted accounts can be recreated with
account_create(removes DELETED flag) - Account data remains accessible during the transaction even after deletion
- The account becomes eligible for garbage collection after transaction completion
Deletion vs Compression
Section titled “Deletion vs Compression”| Operation | Purpose | Requirements | Effect |
|---|---|---|---|
| Delete | Remove account | Zero balance, ownership | Sets DELETED flag |
| Compress | Archive account | State proof | Sets COMPRESSED flag |
Example
Section titled “Example”#include <thru-sdk/c/tn_sdk_syscall.h>
// Delete an account (must have zero balance)ulong account_idx = 2;
// First ensure the account has zero balance// (transfer out any remaining balance first)
ulong result = tsys_account_delete(account_idx);
if (result == TN_VM_SYSCALL_SUCCESS) { // Account marked as deleted // Will be garbage collected after transaction // Can still access data during this transaction // Can be recreated with account_create}