Soft Delete Support
Configure how ElasticLens handles soft-deleted Base Models.
Default Behavior
Section titled “Default Behavior”By default, when a Base Model is soft-deleted, its corresponding Index Model record is deleted. This means soft-deleted records are not searchable.
Keeping Index Records on Soft Delete
Section titled “Keeping Index Records on Soft Delete”If you want soft-deleted records to remain in the index (with their deleted_at timestamp synced), enable the index_soft_deletes config option:
Global Configuration
Section titled “Global Configuration”return [ 'index_soft_deletes' => true, // ...];When enabled, soft-deleting a model will trigger an index rebuild instead of a deletion, syncing the deleted_at field to the index.
Per-Model Override
Section titled “Per-Model Override”You can override the global setting on individual Index Models using the $indexSoftDeletes property:
class IndexedUser extends IndexModel{ protected $baseModel = User::class;
// Override global config for this index protected ?bool $indexSoftDeletes = true;}The per-model setting takes priority over the global config:
$indexSoftDeletes = true- Always keep index records on soft delete$indexSoftDeletes = false- Always remove index records on soft delete$indexSoftDeletes = null(default) - Fall back to globalindex_soft_deletesconfig
Restore Behavior
Section titled “Restore Behavior”When a soft-deleted model is restored, ElasticLens automatically triggers an index rebuild regardless of the soft delete configuration. This ensures the index is always up to date 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.