---
title: account_delete
description: Marks an account as deleted, making it eligible for garbage collection
source_url:
  html: https://thru.org/docs/spec/vm/syscalls/account_delete/
  md: https://thru.org/docs/spec/vm/syscalls/account_delete.md
---

# account_delete

## 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

**Code:** `0x06` (`TN_SYSCALL_CODE_ACCOUNT_DELETE`)

## C SDK Function

```c
ulong tsys_account_delete(ulong account_idx);
```

## Arguments

**`account_idx` · *ulong* · **required****

Index of the account to delete. Must be writable by the current program and have zero balance.

## 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 bounds
- `TN_VM_ERR_SYSCALL_ACCOUNT_DOES_NOT_EXIST` (-9) - Account does not exist
- `TN_VM_ERR_SYSCALL_ACCOUNT_NOT_WRITABLE` (-10) - Account not writable by current program
- `TN_VM_ERR_SYSCALL_INVALID_ACCOUNT` (-27) - Account has non-zero balance or non-zero nonce
- `TN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOW` (-48) - State bytes counter overflow during deactivation

## Resource Consumption

### Compute Units

- **Base cost**: `TN_VM_SYSCALL_BASE_COST` (512 units)
- **Additional cost**: None
- **Total cost**: Fixed at 512 units

### 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

- **GDSC (Global Deactivated State Counter)**: Incremented by `TSDK_ACCOUNT_META_FOOTPRINT + account_data_sz` bytes
- **Purpose**: Tracks deactivated state for the account deletion in the global state counter
- **Overflow protection**: Returns `TN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOW` if counter would overflow

## 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

- 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

| Operation | Purpose | Requirements | Effect |
| - | - | - | - |
| Delete | Remove account | Zero balance, ownership | Sets DELETED flag |
| Compress | Archive account | State proof | Sets COMPRESSED flag |

## Example

```c
#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
}
```
