🔥 CodeIgniter Intermediate

What is CodeIgniter 4 soft deletes?

Why Interviewers Ask This

This tests whether you can apply CodeIgniter knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

CodeIgniter 4 models support soft deletes — marking records as deleted without removing them from the database. Enable in your model: protected $useSoftDeletes = true and set protected $deletedField = "deleted_at". Add a nullable deleted_at DATETIME column to your migration. Calling $this->model->delete($id) sets deleted_at to the current timestamp. Normal queries (findAll(), where()) automatically exclude soft-deleted records. Include deleted records: $this->model->withDeleted()->findAll(). Show only deleted: $this->model->onlyDeleted()->findAll(). Restore a record: $this->model->delete($id, false) does a hard delete; to restore, manually update deleted_at to null. Soft deletes are useful for audit trails, trash functionality, and accidentally deleted record recovery.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a CodeIgniter codebase.