DEV Community

Cover image for How to execute delete statements with jpa query
Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.dev

1 1

How to execute delete statements with jpa query

Use the executeUpdate method of the Query interface. It will return the number of deleted entries:

public int deleteOldMessages(int daysBack) {
  var query = em.createQuery("delete from Message m where createdAt < :givenTimestamp");
  query.setParameter(Message.GIVEN_TIMESTAMP, LocalDateTime.now().minusDays(daysBack));

  return query.executeUpdate();
}
Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay