Soft Delete Support
Soft delete a model. Keep the index, or don’t. Your call.
Default Behavior
Section titled “Default Behavior”When a Base Model is soft-deleted, its Index Model record is deleted. Soft-deleted records aren’t searchable by default.
Keeping Index Records on Soft Delete
Section titled “Keeping Index Records on Soft Delete”Want soft-deleted records to stay in the index (with deleted_at synced)? Enable index_soft_deletes:
Global Configuration
Section titled “Global Configuration”return [ 'index_soft_deletes' => true, // ...];When enabled, soft-deleting a model triggers an index rebuild instead of a deletion, syncing deleted_at to the index.
Per-Model Override
Section titled “Per-Model Override”Override the global setting on individual Index Models:
class IndexedUser extends IndexModel{ protected $baseModel = User::class;
// Override global config for this index protected ?bool $indexSoftDeletes = true;}Per-model takes priority over global:
$indexSoftDeletes = true- Always keep index records on soft delete$indexSoftDeletes = false- Always remove index records on soft delete$indexSoftDeletes = null(default) - Fall back to global config
Restore Behavior
Section titled “Restore Behavior”When a soft-deleted model is restored, ElasticLens triggers an index rebuild regardless of config. The index is always current after a restore.
Observer Flow
Section titled “Observer Flow”The observer handles soft deletes through three events:
deleting- If soft delete with index retention: skips deletion, letsdeletedhandle it. Otherwise deletes the index record.deleted- If the model is trashed andshouldIndexSoftDeletes()is true: triggers a rebuild to syncdeleted_at.restored- Always triggers a rebuild to sync the restored state.