# DeleteBehavior

> Marks ActiveRecord models as deleted instead of removing their rows, and restores them from that state.

[API reference](/docs/api/index.md) › [Soft Delete](/docs/extensions/soft-delete/reference/index.md) › DeleteBehavior

| class | `yii2\extensions\softdelete\DeleteBehavior` |
| --- | --- |
| Extends | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/DeleteBehavior.php`](https://github.com/yii2-extensions/soft-delete/blob/main/src/DeleteBehavior.php#L44) |

Marks ActiveRecord models as deleted instead of removing their rows, and restores them from that state.

[`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) writes [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues), [`restore()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-restore) writes the restore values, and
[`safeDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-safeDelete) attempts a physical delete first and marks the record only when the database rejects it with
[`$deleteFallbackException`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-deleteFallbackException). Yii's own [`BaseActiveRecord::delete()`](/docs/api/yii/db/baseactiverecord/index.md#method-delete) keeps deleting the row physically.
[`ScopedQuery`](/docs/extensions/soft-delete/reference/scopedquery/index.md) and [`QueryBehavior`](/docs/extensions/soft-delete/reference/querybehavior/index.md) provide query scoping.

The owner may declare public `beforeSoftDelete()`, `afterSoftDelete()`, `beforeRestore()` and `afterRestore()`
methods; the behavior calls them by name before triggering the matching event. A before-method returning a falsy
value cancels the operation.

Every write targets the row by its persisted primary key and honors [`BaseActiveRecord::optimisticLock()`](/docs/api/yii/db/baseactiverecord/index.md#method-optimisticLock),
whose value must be a non-negative integer that still matches the row and that each update increments. A record
without its primary key loaded, a hook that is not public, or a transactional operation on an owner without a Yii
SQL connection raises [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md).

Exceptions from hooks, event handlers and the database propagate. A rolled-back transaction first returns the owner
attributes to their state before the call; when the rollback itself fails, [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php) reports it with
the exception that caused the rollback as its previous one.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$allowDeleteCallback`](#property-allowDeleteCallback) | `mixed` | Callback that decides whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) deletes the row physically instead of marking it, or `null` to always mark it. Default: `null`. | `DeleteBehavior` |
| [`$deleteFallbackException`](#property-deleteFallbackException) | `string` | Class name of the [`Throwable`](https://www.php.net/manual/en/class.throwable.php) that makes [`safeDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-safeDelete) fall back to a soft delete when the physical delete raises it or one of its subclasses. Default: `\yii\db\IntegrityException::class`. | `DeleteBehavior` |
| [`$invokeDeleteEvents`](#property-invokeDeleteEvents) | `bool` | Whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) runs the owner's [`BaseActiveRecord::beforeDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-beforeDelete) and [`BaseActiveRecord::afterDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-afterDelete). Default: `true`. | `DeleteBehavior` |
| <span id="property-owner"></span>[`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) | `yii\base\T\|null` | The owner of this behavior Default: `null`. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`$restoreAttributeValues`](#property-restoreAttributeValues) | `array<string, mixed>\|null` | Values written on restoration, by attribute name. Default: `null`. | `DeleteBehavior` |
| [`$softDeleteAttributeValues`](#property-softDeleteAttributeValues) | `array<string, mixed>` | Values written on soft deletion, by attribute name, such as a flag or a status:  ```php ['isDeleted' => true] ['statusId' => Item::STATUS_DELETED] ```  A non-scalar callable is a computed value: it receives the owner and returns the value to write. Default: `['isDeleted' => true]`. | `DeleteBehavior` |
| <span id="property-useRestoreAttributeValuesAsDefaults"></span>`$useRestoreAttributeValuesAsDefaults` | `bool` | Whether inserting a record fills its `null` attributes with the restore values. Default: `false`. | `DeleteBehavior` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`afterRestore()`](#method-afterRestore) | Runs the owner's public `afterRestore()`, when declared, and triggers [`EVENT_AFTER_RESTORE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_AFTER_RESTORE). | `DeleteBehavior` |
| [`afterSoftDelete()`](#method-afterSoftDelete) | Runs the owner's public `afterSoftDelete()`, when declared, and triggers [`EVENT_AFTER_SOFT_DELETE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_AFTER_SOFT_DELETE). | `DeleteBehavior` |
| <span id="method-attach"></span>[`attach()`](/docs/api/yii/base/behavior/index.md#method-attach) | Attaches the behavior object to the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`beforeInsert()`](#method-beforeInsert) | Fills the `null` attributes of a record being inserted with the restore values while [`$useRestoreAttributeValuesAsDefaults`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-useRestoreAttributeValuesAsDefaults) is enabled; handles [`BaseActiveRecord::EVENT_BEFORE_INSERT`](/docs/api/yii/db/baseactiverecord/index.md#event-EVENT_BEFORE_INSERT). | `DeleteBehavior` |
| [`beforeRestore()`](#method-beforeRestore) | Runs the owner's public `beforeRestore()`, when declared, and triggers [`EVENT_BEFORE_RESTORE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_BEFORE_RESTORE). | `DeleteBehavior` |
| [`beforeSoftDelete()`](#method-beforeSoftDelete) | Runs the owner's public `beforeSoftDelete()`, when declared, and triggers [`EVENT_BEFORE_SOFT_DELETE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_BEFORE_SOFT_DELETE). | `DeleteBehavior` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detach"></span>[`detach()`](/docs/api/yii/base/behavior/index.md#method-detach) | Detaches the behavior object from the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`events()`](#method-events) | Registers the owner event that can apply active marker defaults before insertion. | `DeleteBehavior` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`resolveRestoreAttributeValues()`](#method-resolveRestoreAttributeValues) | Returns the values written on restoration. | `DeleteBehavior` |
| [`restore()`](#method-restore) | Restores a soft-deleted record by writing the restore values together with its other changed attributes. | `DeleteBehavior` |
| [`safeDelete()`](#method-safeDelete) | Deletes the row physically and marks the record as deleted instead when the database rejects the delete with [`$deleteFallbackException`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-deleteFallbackException). | `DeleteBehavior` |
| [`softDelete()`](#method-softDelete) | Marks the record as deleted by writing [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues) together with its other changed attributes, or deletes it physically when [`$allowDeleteCallback`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-allowDeleteCallback) allows it. | `DeleteBehavior` |
| [`isDeleteAllowed()`](#method-isDeleteAllowed) protected | Returns whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) deletes the row physically, as decided by [`$allowDeleteCallback`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-allowDeleteCallback). | `DeleteBehavior` |
| [`restoreInternal()`](#method-restoreInternal) protected | Writes the restore values together with the other changed owner attributes. | `DeleteBehavior` |
| [`softDeleteInternal()`](#method-softDeleteInternal) protected | Runs the soft-delete hooks around writing [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues) with the other changed attributes. | `DeleteBehavior` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_AFTER_RESTORE"></span>`EVENT_AFTER_RESTORE` | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | Triggered after a record is restored. Event name: `'afterRestore'`. | `DeleteBehavior` |
| <span id="event-EVENT_AFTER_SOFT_DELETE"></span>`EVENT_AFTER_SOFT_DELETE` | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | Triggered after a record is marked as deleted. Event name: `'afterSoftDelete'`. | `DeleteBehavior` |
| <span id="event-EVENT_BEFORE_RESTORE"></span>`EVENT_BEFORE_RESTORE` | [`yii\base\ModelEvent`](/docs/api/yii/base/modelevent/index.md) | Triggered before a record is restored; set [`ModelEvent::$isValid`](/docs/api/yii/base/modelevent/index.md#property-isValid) to `false` to cancel. Event name: `'beforeRestore'`. | `DeleteBehavior` |
| <span id="event-EVENT_BEFORE_SOFT_DELETE"></span>`EVENT_BEFORE_SOFT_DELETE` | [`yii\base\ModelEvent`](/docs/api/yii/base/modelevent/index.md) | Triggered before a record is marked as deleted; set [`ModelEvent::$isValid`](/docs/api/yii/base/modelevent/index.md#property-isValid) to `false` to cancel. Event name: `'beforeSoftDelete'`. | `DeleteBehavior` |

## Property details

### $allowDeleteCallback <span id="property-allowDeleteCallback"></span>

```php
public mixed $allowDeleteCallback = null
```

Callback that decides whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) deletes the row physically instead of marking it, or `null` to always mark it.

The callback receives the owner and must return a `bool`; any other callback
result, or a value that is not callable, makes [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) throw [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md).

```php
static fn (\yii\db\BaseActiveRecord $user): bool => $user->lastLoginDate === null
```

### $deleteFallbackException <span id="property-deleteFallbackException"></span>

```php
public string $deleteFallbackException = \yii\db\IntegrityException::class
```

Class name of the [`Throwable`](https://www.php.net/manual/en/class.throwable.php) that makes [`safeDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-safeDelete) fall back to a soft delete when the physical delete raises it or one of its subclasses.

The default handles database integrity failures such
as a restrictive foreign key. A value that does not name a [`Throwable`](https://www.php.net/manual/en/class.throwable.php) type makes [`safeDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-safeDelete)
throw [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md).

### $invokeDeleteEvents <span id="property-invokeDeleteEvents"></span>

```php
public bool $invokeDeleteEvents = true
```

Whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) runs the owner's [`BaseActiveRecord::beforeDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-beforeDelete) and [`BaseActiveRecord::afterDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-afterDelete).

The soft-delete hooks and events run in either case.

### $restoreAttributeValues <span id="property-restoreAttributeValues"></span>

```php
public array<string, mixed>|null $restoreAttributeValues = null
```

Values written on restoration, by attribute name.

When `null`,
[`resolveRestoreAttributeValues()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-resolveRestoreAttributeValues) infers them from [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues).

### $softDeleteAttributeValues <span id="property-softDeleteAttributeValues"></span>

```php
public array<string, mixed> $softDeleteAttributeValues = ['isDeleted' => true]
```

Values written on soft deletion, by attribute name, such as a flag or a status:  ```php ['isDeleted' => true] ['statusId' => Item::STATUS_DELETED] ```  A non-scalar callable is a computed value: it receives the owner and returns the value to write.

A string is
always a literal value, even when it names a function.

```php
['deletedAt' => static fn (\yii\db\BaseActiveRecord $model): int => time()]
```

## Method details

### afterRestore() <span id="method-afterRestore"></span>

```php
public function afterRestore(): void
```

Runs the owner's public `afterRestore()`, when declared, and triggers [`EVENT_AFTER_RESTORE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_AFTER_RESTORE).

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the owner declares `afterRestore()` with a visibility other than public.

### afterSoftDelete() <span id="method-afterSoftDelete"></span>

```php
public function afterSoftDelete(): void
```

Runs the owner's public `afterSoftDelete()`, when declared, and triggers [`EVENT_AFTER_SOFT_DELETE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_AFTER_SOFT_DELETE).

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the owner declares `afterSoftDelete()` with a visibility other than public.

### beforeInsert() <span id="method-beforeInsert"></span>

```php
public function beforeInsert(yii\base\Event $event): void
```

Fills the `null` attributes of a record being inserted with the restore values while
[`$useRestoreAttributeValuesAsDefaults`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-useRestoreAttributeValuesAsDefaults) is enabled; handles [`BaseActiveRecord::EVENT_BEFORE_INSERT`](/docs/api/yii/db/baseactiverecord/index.md#event-EVENT_BEFORE_INSERT).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$event` | [`yii\base\Event`](/docs/api/yii/base/event/index.md) |  |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the restore values cannot be inferred.

### beforeRestore() <span id="method-beforeRestore"></span>

```php
public function beforeRestore(): bool
```

Runs the owner's public `beforeRestore()`, when declared, and triggers [`EVENT_BEFORE_RESTORE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_BEFORE_RESTORE).

**Returns**

`bool`: Whether the restoration may proceed; `false` when the owner method returns a falsy value, in which case the event is not triggered, or when an event handler invalidates the event.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the owner declares `beforeRestore()` with a visibility other than public.

### beforeSoftDelete() <span id="method-beforeSoftDelete"></span>

```php
public function beforeSoftDelete(): bool
```

Runs the owner's public `beforeSoftDelete()`, when declared, and triggers [`EVENT_BEFORE_SOFT_DELETE`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#event-EVENT_BEFORE_SOFT_DELETE).

**Returns**

`bool`: Whether the soft deletion may proceed; `false` when the owner method returns a falsy value, in which case the event is not triggered, or when an event handler invalidates the event.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the owner declares `beforeSoftDelete()` with a visibility other than public.

### events() <span id="method-events"></span>

```php
public function events(): array<string, string>
```

Registers the owner event that can apply active marker defaults before insertion.

**Returns**

`array<string, string>`: Owner events handled by the behavior.

### resolveRestoreAttributeValues() <span id="method-resolveRestoreAttributeValues"></span>

```php
public function resolveRestoreAttributeValues(): array<string, mixed>
```

Returns the values written on restoration.

Returns [`$restoreAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-restoreAttributeValues) when set. Otherwise infers each value from
[`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues): a `bool` is negated, `0` and `1` swap, and a computed value restores to `null`.

**Returns**

`array<string, mixed>`: Values by attribute name.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When a soft-delete value has no inferable counterpart.

### restore() <span id="method-restore"></span>

```php
public function restore(): false|int
```

Restores a soft-deleted record by writing the restore values together with its other changed attributes.

Runs inside a transaction when the owner declares [`ActiveRecord::OP_UPDATE`](/docs/api/yii/db/activerecord/index.md#const-OP_UPDATE) transactional.

**Returns**

`false|int`: Number of restored rows; `0` for an unsaved record, when no value differs from the loaded ones, or when no row matched; `false` when a hook or event handler cancels the restoration.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the restore values cannot be inferred or an owner requirement is not met.
- [`yii\db\StaleObjectException`](/docs/api/yii/db/staleobjectexception/index.md): When the optimistic lock value is invalid, cannot be incremented, or is outdated.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): When a rollback fails after an exception.

### safeDelete() <span id="method-safeDelete"></span>

```php
public function safeDelete(): false|int
```

Deletes the row physically and marks the record as deleted instead when the database rejects the delete with
[`$deleteFallbackException`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-deleteFallbackException).

Requires a SQL [`ActiveRecord`](/docs/api/yii/db/activerecord/index.md) owner. The physical attempt runs [`BaseActiveRecord::beforeDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-beforeDelete) and
[`BaseActiveRecord::afterDelete()`](/docs/api/yii/db/baseactiverecord/index.md#method-afterDelete) inside a transaction. When the physical delete raises an eligible
exception, that transaction is rolled back and the soft deletion runs in a new one. An exception raised by a
hook is never eligible.

**Returns**

`false|int`: Number of deleted or marked rows; `0` for an unsaved record or when no row matched; `false` when a hook or event handler cancels the operation.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the owner or [`$deleteFallbackException`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-deleteFallbackException) is invalid.
- [`yii\db\StaleObjectException`](/docs/api/yii/db/staleobjectexception/index.md): When the optimistic lock value is invalid, cannot be incremented, or is outdated.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): When a rollback fails after an exception.

### softDelete() <span id="method-softDelete"></span>

```php
public function softDelete(): false|int
```

Marks the record as deleted by writing [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues) together with its other changed
attributes, or deletes it physically when [`$allowDeleteCallback`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-allowDeleteCallback) allows it.

Marking the record runs inside a transaction when the owner declares [`ActiveRecord::OP_DELETE`](/docs/api/yii/db/activerecord/index.md#const-OP_DELETE) or
[`ActiveRecord::OP_UPDATE`](/docs/api/yii/db/activerecord/index.md#const-OP_UPDATE) transactional. The physical delete is [`BaseActiveRecord::delete()`](/docs/api/yii/db/baseactiverecord/index.md#method-delete), which
follows [`ActiveRecord::OP_DELETE`](/docs/api/yii/db/activerecord/index.md#const-OP_DELETE) alone.

**Returns**

`false|int`: Number of affected rows; `0` for an unsaved record, when no value differs from the loaded ones, or when no row matched; `false` when a hook or event handler cancels the operation.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When [`$allowDeleteCallback`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-allowDeleteCallback) is invalid or an owner requirement is not met.
- [`yii\db\StaleObjectException`](/docs/api/yii/db/staleobjectexception/index.md): When the optimistic lock value is invalid, cannot be incremented, or is outdated.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): When a rollback fails after an exception.

### isDeleteAllowed() <span id="method-isDeleteAllowed"></span>

```php
protected function isDeleteAllowed(): bool
```

Returns whether [`softDelete()`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#method-softDelete) deletes the row physically, as decided by [`$allowDeleteCallback`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-allowDeleteCallback).

**Returns**

`bool`: Whether the physical delete is allowed; `false` when no callback is configured.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the callback is not callable or does not return a `bool`.

### restoreInternal() <span id="method-restoreInternal"></span>

```php
protected function restoreInternal(): int
```

Writes the restore values together with the other changed owner attributes.

**Returns**

`int`: Number of restored rows.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the restore values cannot be inferred or the primary key is not loaded.
- [`yii\db\StaleObjectException`](/docs/api/yii/db/staleobjectexception/index.md): When the optimistic lock value is invalid, cannot be incremented, or is outdated.

### softDeleteInternal() <span id="method-softDeleteInternal"></span>

```php
protected function softDeleteInternal(): false|int
```

Runs the soft-delete hooks around writing [`$softDeleteAttributeValues`](/docs/extensions/soft-delete/reference/deletebehavior/index.md#property-softDeleteAttributeValues) with the other changed attributes.

**Returns**

`false|int`: Number of rows marked as deleted, or `false` when a hook or event handler cancels.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the primary key is not loaded or an owner hook is not public.
- [`yii\db\StaleObjectException`](/docs/api/yii/db/staleobjectexception/index.md): When the optimistic lock value is invalid, cannot be incremented, or is outdated.