Bulk Delete Using Spring JPA CriteriaDelete


The new feature bulk delete operation in Criteria API was added in JPA 2.1. I will show you an example how to perform bulk delete using Spring JPA CriteriaDelete. The CriteriaDelete interface can be used to implement bulk delete operations.

Delete operations performed through CriteriaDelete interface are directly mapped to database delete operations. Hence the persistent context is not synchronized with the result and optimistic lock is not available for the involved entities. Therefore, if you are using optimistic lock then you need to update the version column as part of your delete statement.

Related Post:

jpa criteriadelete


Java 1.8+, Maven 3.8.2, Spring Boot & Spring Data JPA 2.5.6, MySQL 8.0.26

Project Setup

You can create a maven-based project in your favorite IDE or tool. The name of the project is spring-data-jpa-criteriadelete.

You can use the following pom.xml file for your maven project. The following build file has the required dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">








MySQL Table

For this example, I am going to create a table called product under roytuts database. So create a table product under database roytuts in the MySQL server with the below structure.

CREATE TABLE `product` (
	`id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT,
	`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
	`price` double COLLATE utf8mb4_unicode_ci NOT NULL,
	`sale_price` double COLLATE utf8mb4_unicode_ci NOT NULL,
	`sales_count` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL,
	`sale_date` VARCHAR(20) NOT NULL,

I am also going to store sample data into the product table to test the application right away.

insert into product(id,name,price,sale_price,sales_count,sale_date) values(1, 'Desktop','30000','35000','55','02-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(2, 'Desktop','30300','37030','43','03-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(3, 'Tablet','39010','48700','145','10-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(4, 'Phone','15000','17505','251','05-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(5, 'Phone','18000','22080','178','05-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(6, 'Tablet','30500','34100','58','05-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(7, 'Adapter','2000','2500','68','06-10-2021');
insert into product(id,name,price,sale_price,sales_count,sale_date) values(8, 'TV','45871','55894','165','07-10-2021');


The following datasource configuration is done in the src/main/resources/application.properties file:




I have set the following properties so that Hibernate does not create the product table because I already have created the table in MySQL server.


I am logging SQL statement and formatting them for better reading in the console using the following key/value pairs:


Entity Class

The entity class maps the Java object to the table in database.

@Table(name = "product")
public class Product {

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	private String name;
	private double price;
	private double salePrice;
	private int salesCount;
	private String saleDate;

               //Getters and Setters

Service Class

The service class is responsible for processing the business of the application.

I have included the EntityManager instance from the persistent context which will be used for building CriteriaDelete query.

An example for bulk delete may be, you want to delete all data which are older than 90 days. In this example, I am going to delete products. Though this is a simple example and in your real project you may have thousands or lakhs of records for which you want to perform bulk delete operations.

public class DeleteService {

	private EntityManager entityManager;

	public void deleteBulkProducts(final String productName) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaDelete<Product> criteriaDelete = criteriaBuilder.createCriteriaDelete(Product.class);

		Root<Product> root = criteriaDelete.from(Product.class);

		criteriaDelete.where(criteriaBuilder.equal(root.get("name"), productName));



I am updating the product’s price based on product’s name.

@Transactional annotation from Spring framework is required on the method that performs bulk delete operations, otherwise you may get the following exception:

javax.persistence.TransactionRequiredException: Executing an update/delete query

Spring Boot Main Class

Spring Boot application needs a class with main method and @SpringBootApplication annotation that will deploy the application in embedded Tomcat server.

I don’t want to use the web feature, so I am implementing CommandLineRunner interface. I also included the base package for entity class so that Spring picks it up when required.

@EntityScan(basePackages = "com.roytuts.spring.data.jpa.criteriadelete.entity")
public class CriteriaDeleteApp implements CommandLineRunner {

	private DeleteService deleteService;

	public static void main(String[] args) {
		SpringApplication.run(CriteriaDeleteApp.class, args);

	public void run(String... args) throws Exception {
		System.out.println("CriteriaDelete::Bulk Delete Started...");
		System.out.println("CriteriaDelete::Bulk Delete Done.");


Testing the CriteriaDelete Application

Before delete operation the records you will find in the product table are given below:

jpa criteriadelete

Run the above main class as Java Application or Spring Boot Application. Your products having name Desktop will be deleted.

After delete operation you will find the updated price for Desktop product:

criteriadelete jpa

You will also find the following log in the console for the SQL statement.

CriteriaDelete::Bulk Delete Started...
o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Desktop]
CriteriaDelete::Bulk Delete Done.

Hope you got idea how to use CriteriaDelete API with Spring Boot to delete multiple records from database.

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *