Spring Data JPA deleteBy List of IDs

When building applications, there might be scenarios where you need to delete multiple entities based on a list of their identifiers. Using Spring Data JPA, this can be achieved seamlessly, and in this blog post, we'll explore how.

Create JPA Entity

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Table(name = "persons")
public class Person {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

Create Spring Data JPA Repository - PersonRepository

Let's create an PersonRepository interface that extends the JpaRepository interface from Spring Data JPA:

import com.springdatajpa.springboot.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository; public interface PersonRepository extends JpaRepository<Person, Long> {

Deleting Entities by List of IDs 

With the repository in place, Spring Data JPA provides a convention-based method to delete entities by their IDs:
void deleteByIdIn(List<Long> ids);
It's that simple! The deleteByIdIn is a derived delete query, which means that Spring Data JPA automatically creates the necessary query for deleting multiple entities based on their IDs.

Implementing in the Service Layer 

To ensure transactional integrity and separation of concerns, wrap your data operations inside a service:
public class PersonService {

    private PersonRepository personRepository;

    public void deletePersonsByListOfIds(List<Long> ids) {
The @Transactional annotation ensures that all deletion operations in the list occur within a single transaction. If an error arises with any of the deletions, the entire operation will be rolled back, maintaining data consistency. 

Safety Considerations 

Validation: Always ensure that the list of IDs passed is valid. Null or invalid values can cause unexpected behaviors.

Database Constraints: Be wary of database constraints that might prevent deletions, such as foreign key constraints. Handling such exceptions and providing meaningful feedback is essential. 

Soft Deletion: If the possibility of data recovery is essential, consider implementing a soft deletion mechanism where records are marked as deleted without physically removing them from the database.

Testing the Implementation 

With Spring Boot, it's straightforward to test the deletion logic:
public class PersonServiceTest {

    private PersonService personService;
    private PersonRepository personRepository;

    public void testDeleteByListOfIds() {
        // Given: Initial data
        Person john = personRepository.save(new Person("John", "123 Elm Street"));
        Person jane = personRepository.save(new Person("Jane", "456 Maple Avenue"));

        List<Long> idsToDelete = Arrays.asList(john.getId(), jane.getId());

        // When: Deleting persons by IDs

        // Then: Assert that the persons are deleted


Spring Data JPA's out-of-the-box capabilities are a boon for developers. It simplifies many database operations, allowing developers to focus more on the business logic. When deleting entities using a list of IDs, it is not just about the simplicity but also about the performance gains, as the framework can optimize the deletion process.