Introduction
In database design, deleting records permanently is not always desirable, especially in systems that require auditability, data recovery, or regulatory compliance. Soft delete is a commonly used technique in SQL Server where records are not physically removed from the table but are instead marked as deleted using a flag column.
A soft delete typically involves adding a column such as IsDeleted (bit) or DeletedAt (datetime) to indicate whether a record is considered deleted.
In practical backend development:
Soft delete preserves historical data
Records remain in the database but are excluded from active queries
Enables recovery and auditing
This approach is widely used in enterprise applications, financial systems, and APIs.
Step 1: Modify Table Structure
Add a flag column to indicate deletion status.
ALTER TABLE Employees
ADD IsDeleted BIT DEFAULT 0;
Optional: Add timestamp for better tracking.
ALTER TABLE Employees
ADD DeletedAt DATETIME NULL;
Step 2: Insert Data (Normal Records)
INSERT INTO Employees (Name, IsDeleted)
VALUES ('John Doe', 0);
Step 3: Perform Soft Delete
Instead of deleting the record, update the flag.
UPDATE Employees
SET IsDeleted = 1,
DeletedAt = GETDATE()
WHERE Id = 1;
This marks the record as deleted without removing it from the database.
Step 4: Modify Queries to Exclude Deleted Records
SELECT * FROM Employees
WHERE IsDeleted = 0;
This ensures that soft-deleted records are not visible in application queries.
Step 5: Restore Soft Deleted Record
UPDATE Employees
SET IsDeleted = 0,
DeletedAt = NULL
WHERE Id = 1;
This restores the record.
Step 6: Permanent Delete (Optional)
DELETE FROM Employees
WHERE IsDeleted = 1;
Used in cleanup jobs or archival processes.
Real-Life Examples and Scenarios
Scenario 1: E-commerce Order Management
Orders are rarely deleted permanently.
Soft delete ensures order history is preserved
Useful for audits and dispute resolution
Scenario 2: User Account Deactivation
When a user deletes their account:
Data is marked as deleted
Can be restored if needed
Scenario 3: Audit and Compliance Systems
Industries like finance and healthcare require full data traceability.
Soft delete supports compliance requirements

Join the conversation! Your thoughts help the community grow.