Skip to content

Soft Delete Support

Soft delete a model. Keep the index, or don’t. Your call.

When a Base Model is soft-deleted, its Index Model record is deleted. Soft-deleted records aren’t searchable by default.

Want soft-deleted records to stay in the index (with deleted_at synced)? Enable index_soft_deletes:

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

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

When a soft-deleted model is restored, ElasticLens triggers an index rebuild regardless of config. The index is always current 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.