Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A derived method void deleteByStatus(Status s) on a JpaRepository fails with 'No EntityManager with actual transaction available for current thread' when called from a non-transactional service. Why, and what does it actually do when it works?

A derived delete method needs an active transaction because it loads the matching entities and removes them through the EntityManager, and methods we declare on the interface are not covered by the @Transactional annotations on SimpleJpaRepository's own methods; called from a non-transactional service, there is no transaction, so the EntityManager refuses. Annotating the service method or the repository method with @Transactional fixes it, and @Modifying is not needed for derived deletes. When it works, it selects every matching entity and removes them one by one, so callbacks and cascades run; for large deletes a bulk @Modifying query is better.

Why this question is asked

This error message is one of the first walls people hit with Spring Data: findByStatus works without any annotation, so why does deleteByStatus not? Interviewers ask it because the answer shows whether we understand where Spring Data's transactions come from, and whether we know that a derived delete is not the single DELETE statement its name suggests. In this answer, we will recap how repository transactions work, reproduce the error, explain why it happens, fix it, look at the SQL a derived delete really runs, and compare it with a bulk delete.

1A Quick Recap: Where Repository Transactions Come From

A transaction groups database work so it commits or rolls back as one unit. In Spring, a method gets a transaction when it is called through a proxy, a wrapper object Spring creates at runtime, and the method or its class carries @Transactional.

Spring Data's base implementation, SimpleJpaRepository, is annotated @Transactional(readOnly = true) at class level, and its writing methods such as save, delete and deleteById are annotated @Transactional again without readOnly. That is why repository.save(x) works from code that has no transaction: the repository method starts its own.

The methods we declare on our interface, derived queries and @Query methods, are not methods of SimpleJpaRepository. Spring Data applies the class-level default to them, so query methods run in a read-only transaction, which is enough for reads. Writing methods we declare get no write transaction unless we ask for one. The details have shifted between Spring Data versions, so check your version, but the practical rule is stable: a derived delete needs a transaction from us.

2Reproducing the Error

Here is the failing case: a derived delete called from a service that has no @Transactional.

Java
interface OrderRepository extends JpaRepository<Order, Long> {
void deleteByStatus(Status status);
}
@Service
class CleanupService {
private final OrderRepository orders;
CleanupService(OrderRepository orders) { this.orders = orders; }
public void purgeCancelled() { // no @Transactional
orders.deleteByStatus(Status.CANCELLED);
}
}
// cleanupService.purgeCancelled():
// org.springframework.dao.InvalidDataAccessApiUsageException:
// No EntityManager with actual transaction available for current thread -
// cannot reliably process 'remove' call

Notice the last words of the message: cannot reliably process remove call. The failure is not in a DELETE statement. It is the EntityManager refusing to call remove() on an entity, because remove() changes managed state, and the shared EntityManager Spring injects only allows that inside a transaction.

3What a Derived Delete Actually Does

The name suggests delete from orders where status = ?, but that is not what runs. Spring Data implements a derived delete by first running the matching select, then calling entityManager.remove() on each entity it found, and returning. The removes become DELETE statements when the transaction flushes.

Step throughdeleteByStatus(CANCELLED) inside a transaction
ServiceRepository proxyEntityManagerDatabase
deleteByStatus(CANCELLED)
run derived select
select * from orders where status = ?
3 orders
remove(order 1), remove(order 2), remove(order 3)
run @PreRemove callbacks, cascade to lines
commit -> flush
delete order_line ...; delete from orders where id = ? (x3)

8 messages in order. Play them, or step through them one at a time.

The derived delete loads entities and removes them one at a time.

4Fixing the Error

The fix is to give the call a transaction. The usual place is the service method, because that is where the unit of work is defined and where other related changes may join the same transaction.

Java
@Service
class CleanupService {
@Transactional
public void purgeCancelled() {
orders.deleteByStatus(Status.CANCELLED);
}
}
// Or on the repository method itself:
interface OrderRepository extends JpaRepository<Order, Long> {
@Transactional
void deleteByStatus(Status status);
@Transactional
long deleteByCreatedAtBefore(Instant t); // returns the number of entities removed
}
// select ... from orders where status = ?
// delete from orders where id = ? -- once per matching order

Notice that we do not need @Modifying here. @Modifying is for @Query methods that run an update or delete statement; it tells Spring Data to call executeUpdate instead of running a select. A derived delete is not a statement query at all, it is a select followed by removes, so @Modifying has no role. A derived delete may return void, the count as long, or the list of removed entities.

5When It Works, Should We Use It?

Because each entity goes through remove(), everything JPA does on removal happens: @PreRemove and @PostRemove callbacks run, entity listeners such as auditing run, cascades to children with CascadeType.REMOVE run and orphan removal works. That is the right behaviour when deletion has domain meaning, such as removing an order and its lines while publishing an event.

It is the wrong behaviour for 50,000 cancelled orders. Every matching entity is loaded into memory, and each causes its own DELETE, plus DELETEs for its children. For a large cleanup, a bulk JPQL delete runs one statement instead.

Java
interface OrderRepository extends JpaRepository<Order, Long> {
@Transactional
@Modifying(clearAutomatically = true)
@Query("delete from Order o where o.status = :status")
int bulkDeleteByStatus(@Param("status") Status status);
}
// delete from orders where status = ? -- one statement
// returns 50000

The bulk version skips callbacks, cascades and orphan removal, so child rows must be deleted first with their own bulk statement or by an ON DELETE CASCADE foreign key. clearAutomatically clears the persistence context afterwards so the transaction does not keep using entities that no longer exist.

6Follow-up questions

Why does findByStatus work without @Transactional when deleteByStatus does not?

Reading does not need a transaction in the same way: the shared EntityManager can run a query outside one, and Spring Data applies its read-only default to query methods. Removing entities changes managed state, and the EntityManager only allows that inside an active transaction, so the derived delete fails.

Would @Transactional on a private service method help?

No. Spring's proxy-based transactions only apply to calls that come in through the proxy to an overridable method. A private method, or a public one called from another method of the same class, runs without a transaction. Put it on a public method called from another bean, or on the repository method.

Is a derived delete affected by the self-invocation problem?

Not on the repository side, because the service calls the repository through its proxy. The problem appears only if the @Transactional method on the service is called from inside the same service class.

7Conclusion

In this answer, we saw that a derived delete loads the matching entities and calls remove() on each, which requires a transaction, while the transactional annotations on SimpleJpaRepository do not give our own declared methods a write transaction. Adding @Transactional to the service or repository method fixes the error, @Modifying is not needed, and because the derived delete runs callbacks and cascades one entity at a time, large deletes belong in a bulk @Modifying query.

What to say in the interviewAbout 90 seconds aloud

Open with

The error happens because a derived delete is not a single delete statement. Spring Data runs the matching select, gets the entities, and calls entityManager.remove on each one.

Then, if they want more

Remove changes managed state, and the shared EntityManager only allows that inside an active transaction, so without one it throws, and the message even says it cannot process the remove call. Why does save work without a transaction? Because save is a method of SimpleJpaRepository, which is annotated @Transactional for its writing methods.

Methods I declare on the interface are not covered by those annotations; at most they get the read-only default, which is fine for find methods but not for a delete. The fix is to add @Transactional, ideally on the service method that defines the unit of work, or on the repository method itself.

I do not need @Modifying, because that is only for @Query methods that run update or delete statements; the derived delete is a select plus removes. When it works, it loads every matching entity and removes them one by one, so @PreRemove callbacks, entity listeners, cascades and orphan removal all run. That is good when deletion has business meaning.

For a large cleanup, say tens of thousands of rows, I would use a @Modifying @Query with a JPQL delete instead, which is one statement, remembering that it skips callbacks and cascades, so children must be handled separately.

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.
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 "A derived method void deleteByStatus(Status s) on a JpaRepository fails with 'No...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/496f104a9b/