Quick revision
The short answer
A cascaded remove makes Hibernate load every order, line and payment of the customer into the persistence context and delete them one row at a time, so a customer with 5,000 orders becomes tens of thousands of selects and deletes in one long transaction. For large trees we delete in bulk from the leaves up (payments, lines, orders, then the customer) or let the database do it with ON DELETE CASCADE, declared with Hibernate's @OnDelete, and we run big deletes in batches. We also ask whether a hard delete is needed at all, and we clear the persistence context and mind the second-level cache after bulk operations.
What to remember
In this answer, we saw that a cascaded remove loads the whole customer tree into the persistence context and deletes it row by row in one transaction, which takes minutes for large customers. We replaced it with bulk deletes from the leaves up or a database ON DELETE CASCADE declared with @OnDelete, ran the work in short batches from a background job, cleared the persistence context and evicted second-level cache regions after bulk operations, and asked whether anonymising or archiving would meet the real requirement better than a hard delete.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A cascaded remove makes Hibernate load every order, line and payment of the customer into the persistence context and delete them one row at a time, so a customer with 5,000 orders becomes tens of thousands of selects and deletes in one long transaction. For large trees we delete in bulk from the leaves up (payments, lines, orders, then the customer) or let the database do it with ON DELETE CASCADE, declared with Hibernate's @OnDelete, and we run big deletes in batches. We also ask whether a hard delete is needed at all, and we clear the persistence context and mind the second-level cache after bulk operations.
Why this question is asked
Cascade ALL on a parent-child mapping is convenient for small aggregates and dangerous for big ones, and the danger shows up only when a large customer finally gets deleted, often years after the mapping was written. Interviewers use this scenario to see whether we can explain what Hibernate actually does during a cascaded remove and choose a better mechanism for large data. In this answer, we will recap how cascade remove works, trace the delete step by step to see where the minutes go, replace it with bulk deletes or a database cascade, run the work in batches, handle caches and in-memory state, and question the hard delete itself.
1A Quick Recap: What Cascade Remove Does
With cascade = CascadeType.ALL on Customer.orders, calling em.remove(customer) also removes each order, and each order's cascade removes its lines and payments. Hibernate implements this in memory, through the persistence context, the set of entities it is managing. To remove an entity, it must be managed, so Hibernate loads it; to cascade to its children, it loads the children too. Then it calls lifecycle callbacks, and at flush it issues one DELETE per entity, children before parents so that foreign keys are never violated.
2Tracing the Slow Delete
Consider a wholesale customer with 5,000 orders, each with on average 8 lines and 1 payment. Here is roughly what happens.
6 messages in order. Play them, or step through them one at a time.
All work flows through the application, one entity at a time.
Each part hurts. The selects run one collection at a time, which is N+1 at scale. Every loaded entity takes memory and a dirty-checking snapshot. The deletes are individual statements (batched only if JDBC batching is configured). And the whole thing runs in one transaction, so row locks on 50,000 rows are held until commit, the database's undo or WAL grows, and other transactions touching those orders wait. A transaction timeout of, say, 60 seconds is then easy to exceed.
3Fix 1: Bulk Deletes From the Leaves Up
For large trees we skip the persistence context and tell the database what to delete with set-based statements. The order matters: children first, so that foreign keys are satisfied at every step.
Four statements replace 55,000, and nothing is loaded into memory. The cost is that bulk statements bypass cascades, @PreRemove callbacks, entity listeners and Envers auditing. If some of those matter, for example an audit record per deleted payment, we write that explicitly, for instance with an INSERT ... SELECT into an audit table before the delete.
4Fix 2: Let the Database Cascade
Alternatively, the foreign keys themselves can carry ON DELETE CASCADE, so deleting the customer row makes the database remove orders, then lines and payments, internally. In Hibernate we declare it with @OnDelete(action = OnDeleteAction.CASCADE) on the association, which also lets Hibernate skip loading those children when the parent is removed. Check your Hibernate version for exactly which side of the association accepts @OnDelete.
This is fast and simple, with the same trade-off as bulk deletes: the application does not see the child rows go, so callbacks and auditing do not run for them. It also hides the size of the operation inside one statement, which leads to the next point.
5Running Large Deletes in Batches
Whether we use bulk statements or a database cascade, deleting 50,000 rows in one transaction still holds many locks and produces a large transaction. Better is to delete in chunks with a commit after each one: for example, delete the orders of the customer 500 at a time, each chunk deleting its payments and lines first, until no orders remain, and only then delete the customer. Each transaction is short, locks are released quickly, replication lag stays low, and a failure part-way leaves the work resumable. Such deletes are usually run by a background job rather than inside a user's request, with the customer marked as being deleted in the meantime.
- Is a hard delete really required?YesContinueNoSoft delete or archive instead
- Mark customer as deletinghidden from the UI
- Pick next 500 ordersbackground job
- Delete payments, lines, ordersbulk, children first, commit
- Any orders left?YesPick the next chunkNoDelete the customer row
- Evict cachesentity regions for these types
6 steps in order. Play them, or step through them one at a time.
Short transactions, leaves first, and a decision before any of it.
6Caches, In-Memory State, and the Hard Delete Question
Bulk operations happen behind the persistence context and the second-level cache. Entities already loaded in the current session are now stale, which is why the example calls em.clear(). Hibernate invalidates the affected second-level cache regions for JPQL bulk statements, but for native SQL or database cascades it may not know which regions changed, so we evict the regions for Customer, Order, OrderLine and Payment explicitly (or declare the affected entities on the native query). Otherwise another request may read a cached order that no longer exists.
Finally, it is fair to ask why customers are hard deleted at all. Orders and payments are usually financial records that must be kept for years, so deleting them may even be wrong. Often the real requirement is to close the account and erase personal data, which is better served by anonymising the customer and keeping the orders, or by soft delete followed by archival. That changes the problem from deleting a big tree to updating one row.
7Follow-up questions
Would JDBC batching alone fix the cascade?
It helps the delete phase by sending many deletes per round trip, and setting hibernate.jdbc.batch_size is worthwhile anyway. But Hibernate still loads every entity first and holds them in memory, so for tens of thousands of rows bulk statements or database cascades remain far faster.
Is CascadeType.ALL on Customer.orders a mapping mistake?
Usually yes. Orders are not owned by the customer in the composition sense; they have their own lifecycle and must often outlive the customer. Cascade ALL fits Order to OrderLine, not Customer to Order, and with hundreds of orders per customer the collection itself is questionable.
How do we keep the bulk delete correct as the model grows?
With an integration test that builds a customer with every kind of child and asserts that the delete leaves no rows behind and no foreign key errors. When someone adds a new child table, the test fails until the delete handles it.
8Conclusion
In this answer, we saw that a cascaded remove loads the whole customer tree into the persistence context and deletes it row by row in one transaction, which takes minutes for large customers. We replaced it with bulk deletes from the leaves up or a database ON DELETE CASCADE declared with @OnDelete, ran the work in short batches from a background job, cleared the persistence context and evicted second-level cache regions after bulk operations, and asked whether anonymising or archiving would meet the real requirement better than a hard delete.
Open with
What's happening is that CascadeType.REMOVE works through the persistence context. To remove the customer, Hibernate loads all its orders, then for each order loads its lines and payments, runs callbacks, and at flush issues one delete per row, children first.
Then, if they want more
For a customer with 5,000 orders that's around 10,000 selects and 50,000 deletes, tens of thousands of managed entities in memory, and one long transaction holding locks on all those rows, so it times out. For large trees I'd stop using the cascade for this.
One option is bulk JPQL or SQL deletes in child-to-parent order: payments, then lines, then orders, then the customer, a handful of statements instead of thousands. Another is ON DELETE CASCADE on the foreign keys, which Hibernate can declare with @OnDelete, so the database removes the tree.
Either way, callbacks and Envers don't see the child deletes, so I'd add explicit auditing if it matters. I'd run it in batches, say 500 orders per transaction, from a background job, to keep locks and transactions small. After bulk operations I'd clear the persistence context and evict second-level cache regions for those entities.
And I'd question the requirement: orders and payments are usually records we must keep, so anonymising or soft-deleting the customer might be the right answer.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Deleting a Customer with CascadeType.ALL on orders, and orders cascading to lines and...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/7e22823940/