For audit purposes, your application requires a "soft delete" feature, where records are marked as deleted instead of being removed from the database. How would you implement this feature in your Spring Boot application?

Soft delete is a technique where records are marked as deleted instead of being physically removed from the database. This approach preserves historical data for auditing, reporting, compliance, and recovery purposes while keeping deleted records hidden from normal application operations.

Key Points: • Soft delete retains records in the database for audit and recovery purposes. • A flag or timestamp column is commonly used to identify deleted records. • Application queries must exclude deleted records by default.

Example: Consider an Employee Management System.

Hard Delete:

DELETE FROM employee WHERE id = 101;

Result: • Record is permanently removed. • Recovery is difficult or impossible.

Soft Delete:

UPDATE employee SET deleted = true WHERE id = 101;

Result: • Record remains in the database. • Record is hidden from regular users. • Audit teams can still access historical information.

Common Implementation Approaches:

1. Boolean Flag

Add a column:

deleted BOOLEAN DEFAULT FALSE

Values:

• false → Active record • true → Deleted record

2. Timestamp-Based Deletion

Add a column:

deleted_at TIMESTAMP NULL

Values:

• NULL → Active record • Timestamp value → Deleted record

This approach also records when the deletion occurred.

Hibernate Implementation:

Code Example:

@Entity
@SQLDelete(sql =
    "UPDATE employee SET deleted = true WHERE id=?")
@Where(clause = "deleted=false")
public class Employee {

    private boolean deleted;
}

Explanation:

@SQLDelete • Overrides the DELETE operation. • Executes an UPDATE instead of DELETE.

@Where • Automatically excludes deleted records from queries.

Benefits: • Minimal repository changes. • Transparent implementation.

Restore Deleted Records:

Example:

UPDATE employee SET deleted = false WHERE id = 101;

The record becomes active again.

Audit Information Enhancement:

Additional Fields:

• deleted_by • deleted_at • delete_reason

Example:

deleted_by = 'admin' deleted_at = '2026-06-26 10:30' delete_reason = 'Duplicate Record'

This improves traceability and compliance.

Repository-Level Filtering:

Example Query:

SELECT *
FROM employee
WHERE deleted = false;

Administrative screens can optionally include deleted records when required.

Real-World Example:

Banking Application:

• Closed accounts remain available for audits. • Transaction history is preserved. • Regulatory requirements are satisfied.

Healthcare Application:

• Patient records are never physically deleted. • Historical information remains accessible for legal compliance.

Best Practices:

• Prefer timestamp-based deletion for audit trails. • Record who performed the deletion. • Exclude deleted records automatically using Hibernate filters. • Provide restore functionality for administrators. • Use hard delete only for temporary or non-critical data.

Interview Tip: A concise interview answer is: To implement soft delete in Spring Boot, I would add a deleted flag or deleted timestamp column and update records instead of physically deleting them. Using Hibernate annotations such as @SQLDelete and @Where allows delete operations to become update statements while automatically excluding deleted records from normal queries, ensuring auditability and easy recovery.