2.10.1.12. Delete By Query
Queries are useful not only for finding objects, but for efficiently deleting them as well. For example, you might delete all records created before a certain date. Rather than bring these objects into memory and delete them individually, JPA allows you to perform a single bulk delete based on JPQL criteria.
Delete by query uses the same JPQL syntax as normal queries, with one exception: begin your query string with the delete keyword instead of the select keyword.
To then execute the delete, you call the following Query method:
public int executeUpdate();
This method returns the number of objects deleted. The following example deletes all subscriptions whose expiration date has passed.
Example 19. Delete by Query
Query q = em.createQuery("DELETE FROM Subscription s WHERE s.subscriptionDate :today");
q.setParameter("today", new Date());
int deleted = q.executeUpdate();