Data management is a fundamental component in software development, especially when handling records that need removal from active use. Instead of permanently deleting records (a method known as “hard delete”), many applications use a technique called “soft delete.” The “soft delete” approach is a widely used solution that marks records as inactive without permanently removing them, enabling easy data recovery and historical tracking.
Currently, unlike Spring Data JPA and Hibernate, Spring Data R2DBC does not offer built-in annotations for automatically handling soft-delete. As a result, developers resort to the use of custom repository implementations or queries to achieve similar functionality.
In this article, we’ll examine soft delete, its benefits, and how to implement it in a Spring WebFlux application with R2DBC.
Soft delete is a data management method where records are flagged as inactive or “deleted” without being removed from the database. Typically, this involves adding a field to the entity, like deleted (a boolean) or deletedDate (a timestamp), to indicate that a record is no longer active. Instead of permanently deleting data, a soft delete marks the record as logically deleted, hiding it from standard queries while preserving it for potential recovery or auditing.
If you’re interested in implementing this yourself, I’ve prepared a starter code — a simple blog application with basic CRUD endpoints and unit test cases. You can access the starter code from my GitHub repository using this link. So, let's get right to it.
To implement this, add a field in your entity class to represent the deletion status. A more effective approach is to create an abstract class (AbstractSoftDeletableEntity), define the deletion status field within it, and have your entity class extend this abstract class. This field could either be a boolean (deleted) indicating whether the record is deleted or a timestamp (deletedDate) to specify when it was deleted. I recommend using a timestamp, as it provides the added detail of when the deletion took place:
Modifying the entity class to extend the AbstractSoftDeletableEntity
creates a level of abstraction and separation of concern, thus adhering to the Single Responsibility Principle of object-oriented design.Step 3: Create a Generic Custom Repository Extending the SimpleR2dbcRepository Many resources on implementing soft delete recommend using a custom repository for each entity, which can be cumbersome and hard to manage when an application has numerous entities. A better approach is to use a generic repository interface while providing custom implementations for the basic methods such as counts, deleteById, deleteAll,
findByIdIn the snippet above, I have the SoftDeleteRepositoryImpl
implementing theStep 4: Modifying the Entity Repository to extend the Custom Repository Interface Finally, modify the main repository interface by extending the SoftDeleteRepository and providing the entity class name and the id data type as the generic type arguments. It also provides a default implementation for the findById, findAll, deleteById, and
deleteAll
Step 5: Implement Soft Delete in the Service Layer
Soft delete is an effective and flexible method for managing data without permanently deleting it, making it ideal for applications needing data recovery, compliance, or historical data tracking. In this guide, we discussed what soft delete is, its benefits, and how it can be implemented in a Spring WebFlux application with R2DBC.
You can find the complete source code on GitHub
The above is the detailed content of Implementing Soft Delete in Spring WebFlux with RC. For more information, please follow other related articles on the PHP Chinese website!