# Deleting and restoring

> Delete, restore and force delete rows by key with destroy, restore and forceDestroy, in one transaction with per-row policy checks.

Three endpoints manage the lifecycle of rows: `destroy` deletes them (a soft delete on a `SoftDeletes` model), `restore` brings soft-deleted rows back, and `forceDestroy` removes them for good.

```php [routes/api.php]
use Lomkit\Rest\Facades\Rest;

Rest::resource('documents', DocumentsController::class)->withSoftDeletes();
```

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      URI
    </th>
    
    <th>
      Endpoint
    </th>
    
    <th>
      Policy ability
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        DELETE
      </code>
    </td>
    
    <td>
      <code>
        /api/documents
      </code>
    </td>
    
    <td>
      <code>
        destroy
      </code>
    </td>
    
    <td>
      <code>
        delete
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        POST
      </code>
    </td>
    
    <td>
      <code>
        /api/documents/restore
      </code>
    </td>
    
    <td>
      <code>
        restore
      </code>
    </td>
    
    <td>
      <code>
        restore
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        DELETE
      </code>
    </td>
    
    <td>
      <code>
        /api/documents/force
      </code>
    </td>
    
    <td>
      <code>
        forceDestroy
      </code>
    </td>
    
    <td>
      <code>
        forceDelete
      </code>
    </td>
  </tr>
</tbody>
</table>

- `destroy` is registered by default. `restore` and `forceDestroy` are opt-in: `withSoftDeletes()` registers both, `withSoftDeletes('restore')` only one.
- `withSoftDeletes()` throws `InvalidResourceException` when the routes are registered if the model does not use `SoftDeletes`.
- `except('destroy')` leaves `destroy` out.
- `restore` and `forceDelete` exist only for a model that uses `SoftDeletes`. On another model, they are not [gates](/authorization/gates), and the [details](/digging-deeper/details) leave them out of `gates` and `authorizations`.

## Request and response

```json [DELETE /api/documents]
{
  "resources": [1, 2]
}
```

```json [200 OK]
{
  "data": [
    {"id": 1, "title": "Annual report", "archived": false},
    {"id": 2, "title": "Budget", "archived": false}
  ],
  "meta": {}
}
```

- The response holds the rows that the endpoint wrote, with the selectable fields of the resource, without gates or aggregates.
- `restore` and `forceDestroy` take the same body and answer the same way. `forceDestroy` accepts live and soft-deleted rows.

## Keys

- `resources` is a list of keys, typed like the `ID` field of the resource: an integer for integer keys, a string, a UUID or a ULID otherwise. A list, an object or `null` as an entry gives a 422 under `resources.0`, such as `The resources.0 field must be an integer.`
- `resources` must be a JSON list of at most `rest.limits.sizes.resources` keys, 1000 by default: `The resources field must be a list.`
- A repeated key gives a 422 on every copy. UUID and ULID keys compare without case.
- The keys are read through the `perimeterQuery()` of the resource, with the global scopes of the model, trashed rows included on `restore` and `forceDestroy`. A key that it does not return gives `The selected resources.0 is invalid.` On `destroy`, a key that is already trashed gives the same 422.
- A key outside the perimeter also dispatches the [`KeysOutOfReach`](/digging-deeper/events-and-hooks#keys-out-of-reach) event. A key refused for another reason, its type or a repeat, dispatches nothing.
- `confirmation` carries the token of a [confirmation](/actions-and-metrics/confirmation#confirming-writes). It must be a string: `The confirmation field must be a string.`
- The endpoints take no `search`. An empty array is accepted, and any other value gives `The search field is prohibited.`, or `The search field must be an array.` for `null`. Nothing is written then.

## Checks and transaction

1. `viewAny` on the resource is checked before validation.
2. Validation reads the rows of the keys, through the perimeter, with one query per 1,000 keys.
3. Every row is checked with `delete`, `restore` or `forceDelete` before any row is written. A denied row answers `403`, and nothing is written.
4. `destroyConfirmation()`, `restoreConfirmation()` or `forceDestroyConfirmation()` can ask for a [confirmation](/actions-and-metrics/confirmation#confirming-writes): the endpoint then answers `428`, and nothing is written.
5. The rows are written in one database transaction, on the connection of the model, in the order of `resources`.

Inside the transaction:

<table>
<thead>
  <tr>
    <th>
      <code>
        destroy
      </code>
    </th>
    
    <th>
      <code>
        restore
      </code>
    </th>
    
    <th>
      <code>
        forceDestroy
      </code>
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        destroyingAll($models)
      </code>
    </td>
    
    <td>
      <code>
        restoringAll($models)
      </code>
    </td>
    
    <td>
      <code>
        forceDestroyingAll($models)
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      per row: <code>
        destroying($model)
      </code>
      
      , <code>
        performDelete()
      </code>
      
      , <code>
        destroyed($model)
      </code>
    </td>
    
    <td>
      per row: <code>
        restoring($model)
      </code>
      
      , <code>
        performRestore()
      </code>
      
      , <code>
        restored($model)
      </code>
    </td>
    
    <td>
      per row: <code>
        forceDestroying($model)
      </code>
      
      , <code>
        performForceDelete()
      </code>
      
      , <code>
        forceDestroyed($model)
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        destroyedAll($models)
      </code>
    </td>
    
    <td>
      <code>
        restoredAll($models)
      </code>
    </td>
    
    <td>
      <code>
        forceDestroyedAll($models)
      </code>
    </td>
  </tr>
</tbody>
</table>

- A failure on any row, in a hook, in a `perform` method or in the database, rolls back the rows written before it, and the request answers with the error.
- An observer or a listener that runs after commit runs only when the transaction commits. The `ResourceDestroyed`, `ResourceRestored` and `ResourceForceDestroyed` events are dispatched once, after the commit.
- With `transactions.attempts` above 1, a deadlock runs the transaction again, see [Deadlock retries](/writing/mutate#deadlock-retries).
- A `perform` method that writes on another connection writes outside the transaction.

See [Events and hooks](/digging-deeper/events-and-hooks).

## Deleting children with their parent

To delete child rows in the same request as an update of their parent, use a `delete` entry in `mutate` on a relation declared `deletable()`, see [Relations](/writing/relations#deleting-child-rows). The `destroy` hooks do not run there.

## Errors

```json [403 Forbidden]
{
  "message": "This action is unauthorized."
}
```

```json [422 Unprocessable Content]
{
  "message": "The selected resources.0 is invalid.",
  "errors": {
    "resources.0": ["The selected resources.0 is invalid."]
  }
}
```

<table>
<thead>
  <tr>
    <th>
      Status
    </th>
    
    <th>
      Cause
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        403
      </code>
    </td>
    
    <td>
      <code>
        viewAny
      </code>
      
       is denied, or a row is denied by <code>
        delete
      </code>
      
      , <code>
        restore
      </code>
      
       or <code>
        forceDelete
      </code>
      
      . The message is the one of the first denied row in the order of <code>
        resources
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        422
      </code>
    </td>
    
    <td>
      A key outside the perimeter, missing, already trashed on <code>
        destroy
      </code>
      
      , repeated, or of the wrong type
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        422
      </code>
    </td>
    
    <td>
      A <code>
        search
      </code>
      
       key that is not an empty array
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        428
      </code>
    </td>
    
    <td>
      The resource asks for a confirmation, see <a href="/actions-and-metrics/confirmation#confirming-writes">
        Confirmation
      </a>
    </td>
  </tr>
</tbody>
</table>

With the `DenialsRenderer` registered, a `403` also lists every denied row under `denials`, with its `path` (`resources.1`), `ability`, `key` and `message`. See [Policies](/authorization/policies).
