What are Soft Deletes?
Soft deletes mark documents as deleted without permanently removing them from Firestore. Instead of erasing data, adeletedAt timestamp is set. This provides:
- Data recovery: Restore accidentally deleted documents
- Audit trails: Track when data was deleted and by whom (with hooks)
- Compliance: Meet regulatory requirements for data retention
- Safety: Prevent permanent data loss from user errors
How It Works
Automatic deletedAt Field
Every document created through FirestoreORM automatically includes adeletedAt field set to null:
From src/core/FirestoreRepository.ts:307-325:
deletedAt: null is added on line 310. This ensures all documents have the field, which is essential for filtering queries.
Soft Delete Implementation
Fromsrc/core/FirestoreRepository.ts:716-731:
- Checks if the document exists (line 721)
- Creates an ISO timestamp (line 723)
- Runs
beforeSoftDeletehooks (line 725) - Updates the
deletedAtfield (line 726) - Runs
afterSoftDeletehooks (line 727)
Automatic Query Filtering
Fromsrc/core/QueryBuilder.ts:787-791:
where('deletedAt', '==', null) unless explicitly requested otherwise.
Basic Operations
Soft Delete a Document
Restore a Deleted Document
Fromsrc/core/FirestoreRepository.ts:861-875:
deletedAt back to null:
Hard Delete (Permanent)
Bulk Operations
Bulk Soft Delete
Fromsrc/core/FirestoreRepository.ts:754-792:
Bulk Restore
Fromsrc/core/FirestoreRepository.ts:892-915:
Purge Deleted Documents
Fromsrc/core/FirestoreRepository.ts:812-840:
Querying Deleted Documents
Include Deleted in Query
Query Only Deleted Documents
Fromsrc/core/QueryBuilder.ts:98-102:
Query Updates with Soft Deletes
Fromsrc/core/QueryBuilder.ts:205-248:
Real-World Use Cases
User Account Deletion
Content Moderation
Order Management
Soft Deletes with Hooks
Combine soft deletes with lifecycle hooks for powerful workflows:Best Practices
1. Use Soft Deletes by Default
Soft deletes should be your default deletion strategy:2. Implement Retention Policies
3. Provide Restore UI
4. Document Your Deletion Policy
5. Monitor Soft Delete Metrics
What’s Next?
- Learn about Lifecycle Hooks to add logic around soft deletes
- Explore Schema Validation for data integrity
- Review the Repository Pattern guide