How to Implement Soft Delete in Spring Data REST

Question

What is the best way to implement soft delete functionality in Spring Data REST?

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private boolean deleted = false; // Soft delete flag

    // Getters and Setters
}

Answer

Soft deleting is a common pattern in applications where you want to retain deleted records for audit or recovery purposes. In Spring Data REST, you can implement soft delete functionality by adding a 'deleted' flag to your entity and customizing the repository behavior to exclude those entities when querying.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByDeletedFalse(); // Fetch only non-deleted users

    @Modifying
    @Query("UPDATE User u SET u.deleted = true WHERE u.id = :id")
    void softDelete(@Param("id") Long id);
}

Causes

  • Lack of traditional DELETE operation required for maintaining data integrity.
  • Need for record retention for regulatory compliance or audit trails.
  • Desire to recover deleted records easily if needed.

Solutions

  • Add a boolean field to your entity indicating whether the record is soft deleted.
  • Override the default query methods in the Spring Data repository to filter out soft-deleted records.
  • Implement custom methods for actual deletion functionality that updates the 'deleted' flag instead of removing the record from the database.

Common Mistakes

Mistake: Not indexing the 'deleted' column, leading to performance issues on large datasets.

Solution: Ensure that your database schema includes indexing on the 'deleted' column to optimize query performance.

Mistake: Excluding soft-deleted records in a way that causes inconsistent application behavior.

Solution: Always check the logic in your repository methods to ensure soft-deleted records are handled correctly.

Helpers

  • Spring Data REST
  • soft delete
  • JPA soft delete
  • Spring Data repository
  • entity soft delete implementation
  • Java persistence

Related Questions

⦿How to Work with an ArrayList of Functions in Java 8

Learn how to effectively utilize an ArrayList of functions in Java 8 with clear examples and expert tips.

⦿How to Run a Main Class from a Subproject in SBT During Compile and Run

Learn how to run a main class from a subproject in SBT effectively including stepbystep instructions and common mistakes to avoid.

⦿How to Call a Method from an Abstract Class with the Same Name in a Real Class?

Learn how to invoke a method from an abstract class when the real class has a method with the same name including examples and debugging tips.

⦿How to Advance to the Next Line When Reading a CSV File in Python?

Learn how to properly read CSV files in Python and resolve common issues with moving to the next line during file reading.

⦿Understanding Final Inner Classes in Java

Explore the concept of final inner classes in Java their properties use cases and best practices in objectoriented programming.

⦿How Serious Are Conflicting Transitive Dependencies in Maven?

Learn the significance of conflicting transitive dependencies in Maven their causes and how to effectively manage them.

⦿How to Fix IntelliJ IDEA 16's JDK 1.8 Resolution Issues

Learn how to resolve JDK 1.8 not being recognized in IntelliJ IDEA 16 with clear steps and examples.

⦿How Can I Hide the Mouse Pointer on Android Devices?

Learn how to effectively hide the mouse pointer on Android devices with expert tips and code snippets.

⦿Where Should Caching Be Implemented: DAO Layer or Service Layer in a Spring MVC Web Application?

Explore the best practices for implementing caching in Spring MVC applications. Should it be at the DAO layer or service layer

⦿How to Serialize and Deserialize a Map to/from a List of KeyValuePairs Using Gson?

Learn how to serialize and deserialize a Map to a list of KeyValuePairs with Gson in Java. Stepbystep guide and code examples included.

© Copyright 2025 - CodingTechRoom.com