0050. Account Deletion
Status
Note (2026-11-20): the delete notes below that count rows in another application's table
or read the portfolio dataset catalog are replaced by
ADR 0060 decision 7: the
database applies that table's key, and a RESTRICT refusal answers 409 with its message.
Accepted (2026-11-11), implemented in 3.3.0 through
issue #58.
Builds on ADR 0049 decision 1
(account history is kept for as long as the account exists) and amends
ADR 0047: the account gets delete
routes, and the row router runs Account's own delete.
Amended 2026-11-23 by ADR 0064: an order has its own
row and an order's identifier is unique across accounts, so order events have a key and
the delete finds an account's orders on Order. The keys table and the order-events
notes below describe the schema before it; see the last implementation note.
Amended 2026-11-19 by issue 91: a delete sends one read and one transaction and asks MetaTables for no impact (decisions 3 and 5), and it can delete the account's portfolios too (decision 9). See the implementation notes.
Context
An Account is the root of its history. These keys reference it, directly or
through its sets:
| Referencing rows | Column | References | On delete |
|---|---|---|---|
AccountHoldingsSet, AccountTargetAllocation, VirtualFund |
account_uid |
Account.uid |
CASCADE |
OrderManager |
target_account_uid |
Account.uid |
CASCADE |
account holdings rows (AccountHoldingsStorage) |
account_uid |
Account.uid |
RESTRICT |
| account holdings rows | holdings_set_uid |
AccountHoldingsSet.uid |
CASCADE |
orders rows (OrdersStorage), trades rows (TradesStorage) |
account_identifier |
Account.unique_identifier |
RESTRICT |
| orders rows | order_manager_identifier |
OrderManager.unique_identifier |
RESTRICT |
PositionSet |
account_target_allocation_uid |
AccountTargetAllocation.uid |
CASCADE |
target-position rows (TargetPositionsStorage) |
position_set_uid |
PositionSet.uid |
CASCADE |
VirtualFundHoldingsSet, virtual-fund holdings rows |
virtual_fund_uid |
VirtualFund.uid |
CASCADE |
VirtualFundHoldingsSet, virtual-fund holdings rows |
source_account_holdings_set_uid |
AccountHoldingsSet.uid |
RESTRICT |
OrderEventsStorage has no foreign key. It names an order only by
order_identifier.
Other applications reference the same tables in two ways (ADR 0047):
- Sidecars. A table keyed one-to-one by a core uid "declares its delete
action". ADR 0047's worked example is an account sidecar on
ms_markets__account.uidwith cascade delete. - Storage instances.
msm.storage_instancesbuilds a provider's copy of a Markets storage in the provider's namespace, migrated by the provider. It copies the core storage's foreign keys with the same delete actions. A provider's instance of the holdings, target-position, virtual-fund holdings, orders or trades storage therefore carries the keys in the table above.
No account can be deleted today:
apps/v1/routers/accounts.pyhas no delete route.POST /api/v1/rows/account/delete/answers501, and throughmsm.client,Account.deleteraisesMarketsOperationUnsupported. The row router runsdeleteonly for resources with anapps/v1service delete and for composites that define their owndelete;_owns_checked_deleteignores adeletedefined on aMarketsMetaTableRowsubclass.- Under the runtime backend,
Account.deleteis the genericMarketsMetaTableRow.delete, which deletes one row. Orders, trades or holdings rows that name the account refuse it through theirRESTRICTkeys. msm.services.delete_accountis the same one-row delete. It and the helpers that delete one target allocation, position set or virtual fund (delete_account_target_allocation,delete_position_set,delete_virtual_fund) have no route and no caller.
Some accounts should not exist at all, for example one registered by mistake or by an integration that has since been removed. ADR 0049 decision 1 forbids removing parts of a kept account's history, and issue #50 was declined for that reason. Deleting the whole account is the only way to remove that history, and it does not exist.
Decision
- An account is deleted with its whole history. A service in
msm.servicesdeletes an account and every row in the Markets tables that references it, in one transaction, in the order the keys require: - the account's orders and trades rows, and the order-event rows of those orders, which must be deleted explicitly because they have no key; then its order managers;
- its virtual funds, with their holdings sets and holdings rows;
- its target allocations, with their position sets and target-position rows;
- its holdings sets and holdings rows;
- the
Account.
It runs on PostgreSQL and on SQLite. On SQLite it is an ordered statement list that MetaTables runs in one transaction (MetaTables ADR 0026), as the account snapshot writes do. If any statement fails, nothing is deleted.
- Markets deletes only its own rows. Every statement the service sends targets a table in the Markets namespace. It never sends a delete to a table in another namespace, a provider's storage instance included. A row in such a table is removed only by a foreign key that the table carries:
- a sidecar's key and delete action are declared by the application that owns the sidecar;
- a storage instance's keys are created by the provider's own migration, with the delete actions of the Markets contract. A provider that declares an instance accepts them.
Rows with no key stay in place, for example order events in a provider's
instance of OrderEventsStorage. This is ADR 0047's rule that Markets does
not delete rows in a table another application owns.
- Other applications' keys are allowed and reported.
- A
CASCADEkey deletes the referencing rows with their parent: a sidecar onAccount.uidgoes with the account, and rows in instances of the holdings, target-position and virtual-fund holdings storages go with their sets. The preflight warns about each such table. (Amended 2026-11-19: the delete response no longer lists them, because the delete no longer asks MetaTables for its impact.) -
A
RESTRICTkey refuses the whole delete, for example a provider's orders or trades instance that still holds rows for the account. The preflight warns about the table, and the delete answers409with the database's message, which on PostgreSQL names the constraint. The provider deletes those rows through its own application first. -
The same delete on both backends.
Account.delete(uid)and a newAccount.bulk_delete(uids=...)call the service. Under the runtime backend they run it in-process. Throughmsm.client, the row router runs them as ordinary operations, asPortfolioGroup.bulk_deletealready calls itsmsm.servicesfunction. The row router runsAccount's owndeleteinstead of answering501. The same call removes the same rows on either backend. -
Preflight. A preflight reports, per table, how many rows the delete would remove, the tables in other namespaces it reaches, and what blocks it. It builds on
msm.services.table_impact.delete_rows_impact, as the asset-category, portfolio and portfolio-group preflights do. The database enforces the same keys when the rows are deleted, so the preflight is advisory. (Amended 2026-11-19: only the preflight asks MetaTables for the impact, which takes MetaTables seconds per table, anddelete_rows_impactreuses an answer for the same table for five minutes. The impact depends on the table, not on the caller: the Markets API reaches MetaTables as one workload identity, ADR 0047.) -
Routes. Following the asset-category and portfolio deletes:
DELETE /api/v1/account/{uid}/;POST /api/v1/account/bulk-delete/;-
POST /api/v1/account/bulk-delete/preflight/. -
Who may delete. The same identities as for a portfolio. The services are wrapped in
registry_write("account", "delete")andregistry_write("account", "bulk-delete"), as the portfolio deletes useregistry_write("portfolio", ...). Any caller the platform admits to the Markets API may delete an account, as it may delete a portfolio.registry_writerecords the acting identity on the audit log. There is no other check. -
No partial deletes. Virtual funds, target allocations, position sets and holdings sets are not deleted from an account that is kept (ADR 0049 decision 1).
virtual-fund,virtual-fund-holdings-set,account-target-allocationandposition-setkeep no remote delete. The one-rowdelete_accountis replaced by the service in decision 1, and the unuseddelete_account_target_allocation,delete_position_setanddelete_virtual_fundhelpers are removed, with their repository counterparts. -
The account's portfolios, on request (added 2026-11-19, issue 91). The portfolio package owns this delete, because the portfolio datasets live there and core
msmdoes not import it (ADR 0019):msm_portfolios.servicesexposesdelete_account_with_portfolios,bulk_delete_accounts_with_portfoliosand their preflights, which take a runtime's context. The account routes run them whendelete_portfoliosis set, off by default: the query parameter ofDELETE /api/v1/account/{uid}/, and the option of the bulk-delete action and its preflight.Account.deleteandAccount.bulk_deletedelete the account alone, under both backends; nothing refuses. The delete composes core's account read and statements with the portfolios' own, so the same transaction also deletes the portfolios that the account's virtual funds or target rows name and that nothing references once the account's history is gone, with all their data, as the portfolio delete removes it (ADR 0049, issue 93): their rows in the seven datasets, then the portfolio, whose catalog row and group memberships go byCASCADE. A portfolio is kept, and the answer says why, when: - account history of another account references it: a virtual fund of another account targets it, or target rows outside this account's position sets name it. Account history is never rewritten (ADR 0049 decision 1);
- the catalog places its values or weights in a table another application owns (ADR 0047). Markets never deletes rows there, and the portfolio row cannot go while they exist;
- a key of a table the read cannot see, such as an uncatalogued instance another application owns, refuses its delete. Then the transaction deletes nothing, and a second one deletes the account alone.
The portfolios are never deleted without the account, and a refused portfolio never keeps the account from being deleted.
Rejected Alternatives
Remove Parts Of A Kept Account's History
Deleting a virtual fund, allocation or snapshot from an account that stays rewrites what the account targeted and how its holdings were attributed. ADR 0049 rejected it for portfolios (issue #50), and the same reasons hold here.
Delete Rows In Other Applications' Tables
Markets could find every table in another namespace that references the account and delete its rows explicitly. That breaks ADR 0047. It would also fail on a hosted runtime, where ADR 0047 grants the Markets workload at most Reader on a provider's tables. The owning application has already said what should happen through the delete action of its key.
Refuse Whenever Another Namespace Is Reached
Refusing any delete that cascades into another namespace overrides the action the owning application declared, and makes a sidecar with cascade delete impossible to use as intended. Reporting the cascade keeps it visible without taking the decision away from the owner.
Keep The Runtime Delete As A One-Row Delete
Account.delete would remove one row in-process and the whole history
remotely, so the same call would do different things on the two backends. It
would also keep failing in-process on any account with orders, trades or
holdings.
Delete In Separate Requests
The portfolio delete removes weights, values and the row in separate requests.
For an account that would leave a partly deleted history whenever a later step
is refused, for example by a RESTRICT key in another namespace. One
transaction leaves either the whole account or nothing.
Delete The Account's Portfolios In Their Own Requests
The account delete could be followed by one portfolio delete per orphaned portfolio. Each is its own read and transaction, so a delete of an account with several portfolios would again take many MetaTables requests, the cost issue 91 removes, and a failure between them leaves the portfolios without the account that used them and without a record of why. One transaction answers what was deleted and what was kept.
A Separate Route For The Account And Its Portfolios
A second delete route would duplicate the account delete's routes and preflight.
The account routes take an option, off by default, which keeps the portfolios as
before. In Python the delete with portfolios is its own function in
msm_portfolios, because core msm cannot import the portfolio datasets
(ADR 0019); Account.delete keeps deleting the account alone.
A Registry Of Portfolio Datasets In Core
Core could keep a list of the portfolio datasets that the portfolio package fills
when it is imported, so that Account.delete could take the option. The delete
would then depend on what the process had imported, the hook ADR 0049 rejected
for Portfolio.delete, and would have to refuse when nothing was registered.
An Ownership Or Admin Check
Accounts have no owner column, and admission to the Markets API is decided by the platform (ADR 0047). A stricter check than the portfolio delete's would make the two registry deletes behave differently with no ms-markets rule behind it.
Consequences
- An account's history can be removed only by deleting the account.
- A provider's orders or trades instance that holds rows for an account blocks its deletion until the provider removes them. Order events in a provider's instance stay after the account is gone.
- Rows in other applications' tables can disappear with an account through
their own
CASCADEkeys. The preflight and the delete response list those tables. Account.deleteunder the runtime backend changes from a one-row delete to the cascading delete.msm.servicesandmsm.repositorieslosedelete_account_target_allocation,delete_position_setanddelete_virtual_fund.- The Markets API gains three account routes. The release is a minor version.
docs/fast_api/v1/accounts.mdand the account knowledge pages describe the delete, its preflight and what blocks it.- MetaTables'
delete_rowsimpact reports both kinds of key from other namespaces: its cascade walk follows every table's declared cascade targets, and it adds arestrictedge for each registered key that targets a table the delete reaches. Tables the caller cannot view appear as opaque nodes. - (Added 2026-11-19, issue 91.) An account and the portfolios only it used can go in one request. A portfolio another account, a virtual fund of another account or another application's table uses stays with all its data, and the answer says why. Without the option nothing changes for portfolios.
Implementation Notes
2026-11-11: 3.3.0
- Statements.
msm.repositories.account_deletioncompiles thirteen statements in decision 1's order and runs them as one MetaTables statement list on both dialects (ADR 0026 allows lists on PostgreSQL too). The last deletes the account only while it still has theunique_identifierread before, and is guarded to one row: an account that vanished in between answers404, one that was renamed answers409, and nothing is deleted either way. Order events are deleted when theirorder_identifierbelongs to one of the account's orders and to no other account's order, because orders are unique per account, not globally. - Counts. A statement list returns only its last statement's rows, so
deleted_rowsand the preflight'srow_countscome from one counting read just before the delete. The same read counts decision 3's Markets-side blockers, which refuse the delete before it is sent: orders of other accounts on its order managers, and virtual-fund holdings sets and rows of other accounts allocated from its holdings sets. - Other namespaces.
account_delete_impactsplits the MetaTables impact by namespace: nodes outside the Markets namespace that the delete reaches areother_namespace_cascades, andrestrictedges whose source is outside it areother_namespace_restricts. Both are warnings, because Markets cannot count rows in those tables. ARESTRICTrefusal from the database becomesAccountDeleteConflictErrornaming those tables. - SQLite. MetaTables' SQLite authorizer is also asked about the reads SQLite
makes to enforce a foreign key, so on the local runtime the delete is refused
while any table the caller cannot read has a key into the account tables, even
with no referencing rows. MetaTables documents this limitation;
MetaTables#105
asks to lift it. The SQLite tests give the identity access to the provider
tables and check separately that no statement names them; a strict
xfailtest turns red once MetaTables lifts the limitation.
Amended in 3.4.1: MetaTables 0.1.64 lifted the limitation (#105), and
ms-markets requires it. The SQLite tests now create the provider tables
outside the identity, and the xfail marker is gone.
- Row router. _owns_checked_delete now accepts any row class that defines
delete itself, so Account.delete and Account.bulk_delete run through the
generic row path, wrapped in registry_write("account", "delete") and
registry_write("account", "bulk_delete"). The HTTP routes use
registry_write("account", "delete") and ("account", "bulk-delete").
2026-11-19: 4.1.1, issue 91
Deleting an account through the Markets API took minutes: each step was its own MetaTables request, about 2 s per statement and about 14 s per delete impact. The delete read the account and then counted its rows, and asked for the impact; the bulk delete asked for it in its preflight and again to delete (issue 91).
- One read.
read_account_deletereplacescount_account_delete_rowsand the separate account read. OneSELECTfrom the account by uid answers whether it exists, its rows per table and the rows of other accounts that block it. Every count is a subquery on the bound account uid alone (the account's identifier is read in the same statement), so the database runs each once whatever the number of result rows. Withdelete_portfoliosthere is one row per portfolio the account's virtual funds or target rows name, with the account history of other accounts that references it, its catalog row's values and weights instances, and its rows in each dataset. Each of those counts reads only its portfolio's rows, through the index revision0031gives the datasets (issue 96, ADR 0049's 4.1.1, issue 96 note). - One transaction. The list is the 14 statements of the 3.3.0 note, then,
with portfolios to delete, one
DELETEper dataset for all of them and the portfolioDELETE: at most 22 of the 32 statements MetaTables allows. Each portfolio statement keeps only the portfolios no virtual fund or target row references, which, after the account's own history is gone, means no other account's. MetaTables checks a statement's row guard right after it runs, not only on the last statement, and rolls everything back on a miss, so the account's guard (exactly the account read before) still guards from the middle of the list. The last statement is guarded to delete every portfolio read as deletable and returns them; a portfolio referenced in between stops it, and the account is then deleted alone. - Refusals. A key the read cannot see refuses the whole list. Markets then
sends the account's 14 statements alone; if a key refuses those too, it is the
account's own and
409says so with nothing deleted. With no portfolio to delete, one delete is one read and one transaction; with a refused portfolio, one read and two transactions. - Deadline. The read and the transaction run with MetaTables' longest
deadline, 60 s (
msm.repositories.base.with_delete_deadline), instead of its 15 s default, which covers the whole list. With portfolios, the read counts and the transaction deletes each portfolio's rows in all seven datasets: millions of rows for a portfolio with years of daily weights over hundreds of assets. - Core and the portfolio package. Core
msmdoes not importmsm_portfolios(ADR 0019), so the delete with portfolios lives inmsm_portfolios.repositories.account_portfolio_deletionandmsm_portfolios.services.account_portfolio_deletion. It extends core's read,account_delete_read_statement, with the portfolio columns, and appends its statements to core'saccount_delete_statements. It runs the transaction through core'srun_account_delete, tells a vanished account from a renamed one withaccount_delete_refused, and answers throughaccount_deleted_response, so both deletes check and answer the same way.ACCOUNT_HISTORY_REFERENCESand theNOT EXISTScondition built from it are core's, so both deletes and the portfolio delete use the same definition. The Markets API's account delete runtime includes the portfolio tables. - No impact in the delete.
delete_accountandbulk_delete_accountsno longer ask MetaTables for the account table's impact, and their responses no longer carryother_namespace_cascades; the preflight still does. The409carries the database's message without the list of tables from the impact.POST /api/v1/account/bulk-delete/asks for the impact once, in its preflight, and a preflight within five minutes reuses it. - Bulk. Each account is still deleted in its own transaction, in the order selected, so a portfolio only selected accounts use goes with the last of them. The preflight reads each account as if it were deleted alone.
- Update statistics. An updater run drops every row it publishes at or before
the last time MetaTables stores for its coordinates. After a statement list
commits, MetaTables rebuilds the stored statistics of each time-index table the
list changed
(MetaTables#117,
mainsequence-metatable0.1.69 and later; ms-markets requires 0.1.74). So an account re-created under the sameunique_identifier, whose connector publishes its orders, trades and events again, and a portfolio re-created under a deleted one's identifier start from scratch.
2026-11-21: 5.0.0, indexes and skipped tables
The 4.1.1 delete was one read and one transaction, but its time still grew with every account's history. Measured on PostgreSQL with 10 accounts of 1,000 holdings sets and 200,000 holdings rows, deleting one account took 6.1 s, 6.0 s of it in one foreign key: eleven keys into the deleted tables had no index that leads with their column, so the database read the whole referencing table once per deleted row, here every holdings row once per holdings set. One account's rows in holdings, allocation lines, virtual-fund holdings, orders and trades were also read through indexes that lead with the time column, which is reading the whole index, and the order events filter read every other account's orders.
- Indexes. Alembic revision
0034adds a non-unique index for each such key and lookup (the 5.0.0 CHANGELOG lists them). The delete then takes 0.11 s on the same rows. A PostgreSQL test checks that every key into the deleted tables leads an index and that the read and each statement reach the six history tables by index condition. - Measured.
tests/msm/repositories/test_postgresql_account_delete_volume.pydeletes one of 10 accounts with a history in every table (1,000 holdings sets of 20 positions, allocations, targets, 1,000 orders with trades and events) and runs MetaTables' statistics rebuild after the transaction. Before revision0034the transaction took 54.6 s, 53.2 s of it in the holdings-set statement; after, 0.9 to 1.6 s, and the rebuilds of the seven time-index tables 1.2 to 2.3 s on one workstation. - Skipped tables.
account_delete_statementstakes the read's counts and leaves out the tables the read found empty; the account's guarded statement always runs. A row written after the read is still handled by its key: aCASCADEkey deletes it with its parent, and aRESTRICTkey refuses the transaction, which deletes nothing. Order events have no key, so their statement runs whenever the account has orders. An account with holdings alone sends 3 statements instead of 14. The refusal of the 4.1.1 note sends the account's statements alone, as counted. - Order events. An event stays when another account's order uses its identifier,
found with
NOT EXISTSthrough the order identifier index instead ofNOT INover every other account's orders. - Update statistics. MetaTables takes the time-index tables a transaction locked for
writing as the tables it changed, and rebuilds each one's statistics by reading the whole
table. A
DELETElocks its table even when it deletes nothing, and so does a key'sCASCADE. Skipping empty tables brings an account with holdings alone from 7 such tables to 2: holdings, and allocation lines through the account'sCASCADE. Rebuilding only what a write changed is MetaTables' to do.
2026-11-23: unreleased, issue 128
The bulk deletes of accounts, asset categories and portfolio groups refused the whole
selection with 409 when one row was missing or blocked, and the account bulk delete
also answered 409 after deleting part of the selection. Deleting a selection is one
operation: what may go goes, and what may not is reported, as the portfolio bulk delete
does since issue 127
(issue 128).
This replaces the 4.1.1 note's "Bulk" bullet.
- One envelope. Every bulk delete answers
200withdetail,deleted_count,deletedandfailed; each item offailednames theuidand the reason. The routes no longer refuse the selection, andblocked_preflight_detailis gone. A preflight answersallowed: truewhen at least one selected row can be deleted. - One transaction for the accounts.
msm.repositories.account_deletionbuilds its statements for a set of accounts (accounts_delete_statements): each statement covers every account withIN (…), and the guarded account statement must delete exactly the accounts read. One account's delete is the same list for one account. Withdelete_portfolios, the portfolios' statements follow in the same list. - Selected together. Each account is still read on its own, but counting the other
selected accounts as deleted with it (
together): their orders, virtual funds and target rows block nothing, and a portfolio only selected accounts use is deleted with them, reported with the first. Before, it went with the last account, and two accounts whose rows referenced each other could not be deleted in one call. - A refusal the reads cannot see. A reference written after the reads, or a key of
an application's own table, refuses the selection's transaction, which deletes
nothing. Each account is then deleted on its own, as
DELETE /api/v1/account/{uid}/deletes one, so only the refused ones are reported. - Asset categories and portfolio groups.
msm.services.bulk_deletion.bulk_delete_rowsdeletes every selected row that exists in one statement, which returns the deleted rows; the others are reported as not found. A database refusal is handled as above. - The table itself. MetaTables' impact blockers (the caller cannot write the table, or the table accepts no writes) report every selected row, and nothing is sent. The routes ask for the impact once and reuse it for five minutes, as the preflights do.
2026-11-23: 6.0.0, ADR 0064
An order has its own row, Order, keyed by its uid, unique across accounts
(ADR 0064). This replaces every note above about
order events having no key and orders being unique per account.
- Keys.
Order.account_identifierreferencesAccount.unique_identifierandOrder.order_manager_identifierreferencesOrderManager.unique_identifier, bothRESTRICT.OrdersTSandOrderEventsTSreferenceOrder.uid;TradesTSkeeps its key to the account and references(uid, account_identifier, asset_identifier)of its order. Every one isRESTRICT. - Statements. The delete removes, in order, the events, trades and state rows of the
account's orders, then the orders (
deleted_rowskeysorder_events,trades,order_states,orders), then the order managers: fifteen tables. An account's orders are theOrderrows that name it; their events and state rows are found by their uid.ordered_by_another_accountis gone, and order events get a statement only when the read counted them, as every other table. - Blockers. "Orders of other accounts on its order managers" are
Orderrows of other accounts. - Other applications. A provider's instance of
OrderEventsTScopies the key intoOrder, so its events now block the delete, as its orders do.