AccountMergeOperation class
Merges an account into another account, transferring all XLM and removing the source account.
This operation permanently deletes the source account from the ledger and transfers its entire XLM balance to the destination account. This is an IRREVERSIBLE operation that completely removes the account from the network.
Operation Process:
- Transfers all remaining XLM from source account to destination
- Permanently removes the source account from the ledger
- Releases the account's base reserve back to the network
- Cannot be undone - the account ID can never be used again
Critical Requirements:
- Source account must have exactly 0 subentries:
- No trustlines (remove all with ChangeTrustOperation)
- No open offers (cancel all with ManageOfferOperation)
- No data entries (remove all with ManageDataOperation)
- No additional signers (remove all with SetOptionsOperation)
- Destination account must exist and be different from source
- Minimum XLM balance will be transferred (at least base reserve)
Security Warnings:
- IRREVERSIBLE: Account deletion cannot be undone
- PERMANENT: Account ID can never be recreated or reused
- ALL XLM is transferred - ensure you intend to close the account
- Verify destination address carefully - typos cannot be corrected
Use Cases:
- Account cleanup: Remove unused or deprecated accounts
- Key rotation: Close old account and move to new keypair
- Account consolidation: Merge multiple accounts into one
- Security: Close compromised accounts after moving funds
- Testing: Clean up test accounts
Example - Simple Account Merge:
var mergeOp = AccountMergeOperationBuilder(
destinationAccountId
).setSourceAccount(sourceAccountId).build();
var transaction = TransactionBuilder(sourceAccount)
.addOperation(mergeOp)
.build();
// Source account will be permanently deleted after submission
Example - Complete Account Closure Workflow:
// Step 1: Remove all trustlines
for (var balance in account.balances) {
if (balance.assetType != 'native') {
var changeTrust = ChangeTrustOperationBuilder(
Asset.createNonNativeAsset(balance.assetCode, balance.assetIssuer),
"0"
).build();
// Add to transaction
}
}
// Step 2: Cancel all offers
for (var offer in account.offers) {
var manageOffer = ManageOfferOperationBuilder(
selling,
buying,
"0",
price
).setOfferId(offer.id).build();
// Add to transaction
}
// Step 3: Remove data entries
for (var entry in account.data) {
var manageData = ManageDataOperationBuilder(
entry.name,
null
).build();
// Add to transaction
}
// Step 4: Merge account
var mergeOp = AccountMergeOperationBuilder(
destinationAccountId
).build();
var transaction = TransactionBuilder(sourceAccount)
.addOperation(changeTrust)
.addOperation(manageOffer)
.addOperation(manageData)
.addOperation(mergeOp)
.build();
See also:
- ChangeTrustOperation to remove trustlines before merging
- ManageSellOfferOperation and ManageBuyOfferOperation to cancel offers before merging
- ManageDataOperation to remove data entries before merging
- SetOptionsOperation to remove signers before merging
- Stellar developer docs
Constructors
- AccountMergeOperation(MuxedAccount _destination)
- Creates an AccountMergeOperation.
Properties
- destination → MuxedAccount
-
The account that receives the remaining XLM balance of the source account.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sourceAccount ↔ MuxedAccount?
-
Optional source account for this operation.
getter/setter pairinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toOperationBody(
) → XdrOperationBody -
Converts this operation to its XDR OperationBody representation.
override
-
toString(
) → String -
A string representation of this object.
inherited
-
toXdr(
) → XdrOperation -
Converts this operation to its XDR representation.
inherited
-
toXdrBase64(
) → String -
Returns base64-encoded Operation XDR object from this operation.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
builder(
XdrOperationBody op) → AccountMergeOperationBuilder - Creates an AccountMergeOperationBuilder from XDR operation body.