11.11. 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, JDO allows you to perform a single bulk delete based on JDOQL criteria.


This feature is only partially implemented in this Kodo release. In particular, note that objects deleted by query are not properly cleared from the datastore cache and that certain event callbacks may not be specification-compliant. Until this feature is fully implemented, we recommend isolating classes subject to delete by query in a separate cache which can be evicted manually or through timeouts.

Delete by query is accomplished through the following Query methods:

public long deletePersistentAll ();
public long deletePersistentAll (Object[] parameters);
public long deletePersistentAll (Map parameters);

These methods work simlilarly to the Query.execute methods. Instead of returning matching objects, however, the deleteAll methods delete the matching instances from the database, returning the number of objects deleted.

The following example deletes all subscriptions whose expiration date has passed.

Example 11.27. Delete By Query

Query query = pm.newQuery (Subscription.class, "expirationDate < :today");
long deleted = query.deletePersistentAll (new Object[] { new Date () });


Skip navigation bar   Back to Top