Retire a Sui Table without stranding the last asset
A table stores its entries separately from its handle. During contract retirement, the final cleanup must account for each value before consuming that handle.
Withdraw entries using known keys
The excerpt assumes ownership of a Table keyed by u64 containing transferable ticket objects, with key 23 as its last remaining entry. Inside the module, remove the value and explicitly transfer it to the authorized recipient.
let ticket = table.remove(23u64);
transfer::public_transfer(ticket, recipient);
assert!(table.is_empty(), 0);
table.destroy_empty();
Check the counter before the destructive step
If a second ticket remains under key 29, is_empty is false and the transaction aborts. Table exposes a length but no iterator returning all its application keys. Maintain a key registry or reconstruct the keys from indexed activity and reconcile the count. For large tables, withdraw in bounded transactions before the final empty-table call.