Skip to content

Soft Delete Support

Configure how ElasticLens handles soft-deleted Base Models.

By default, when a Base Model is soft-deleted, its corresponding Index Model record is deleted. This means soft-deleted records are not searchable.

If you want soft-deleted records to remain in the index (with their deleted_at timestamp synced), enable the index_soft_deletes config option:

config/elasticlens.php
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.

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 global index_soft_deletes config

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.

The observer handles soft deletes through three events:

  • deleting - If soft delete with index retention, skips deletion (lets deleted handle it). Otherwise, deletes the index record.
  • deleted - If the model is trashed and shouldIndexSoftDeletes() is true, triggers a rebuild to sync deleted_at.
  • restored - Always triggers a rebuild to sync the restored state.