# Add Soft Deletes to a Filament Resource

Yes. An agent adds SoftDeletes to a Filament 5 resource: migration, trashed filter, restore and force-delete actions, Pest tests, and a PR passing composer test.

[Run this on Rush](https://cloud.getrush.ai/run/add-soft-delete/filament/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent looks for the pattern the panel already uses before writing a new one, because most Filament apps have soft deletes on some resources and not others. In an app shaped like filamentphp/demo, `composer.json` requires `php ^8.1`, `filament/filament ^5.0`, `laravel/framework ^12.0`, `pestphp/pest ^3.0` and `phpunit/phpunit ^11.0`, and `composer test` runs Pest in parallel followed by PHPStan. `App\Models\Shop\Order`, `Customer`, and the HR `Employee` and `Project` models use the `SoftDeletes` trait, their migrations call `$table->softDeletes()`, and their resources show the full pattern: `OrderResource::getEloquentQuery()` removes `SoftDeletingScope` with `withoutGlobalScopes`, `OrdersTable` lists `TrashedFilter::make()` first in its filters, and `EditOrder::getActions()` returns `RestoreAction` and `ForceDeleteAction` beside `DeleteAction`. `Product` has none of this: `App\Models\Shop\Product` uses `HasFactory` and `InteractsWithMedia`, the `2021_12_13_000014_create_products_table.php` migration has no `deleted_at`, and `EditProduct::getActions()` returns only `DeleteAction::make()`.

The agent adds `use SoftDeletes` to `Product`, mirroring `Order`, and writes a new migration, `add_soft_deletes_to_products_table`, that calls `$table->softDeletes()` in `up` and `dropSoftDeletes()` in `down`, rather than editing the 2021 migration that production has already run. In `ProductResource` it adds `getEloquentQuery()` with `withoutGlobalScopes([SoftDeletingScope::class])`, copied from `OrderResource`, so the table can show trashed rows when the filter asks for them. In `ProductsTable` it inserts `TrashedFilter::make()` ahead of the existing `QueryBuilder` filter, and to the `recordActions` `ActionGroup` it adds `RestoreAction::make()` and `ForceDeleteAction::make()` after the existing `DeleteAction`, which already fires a `Notification`. In `groupedBulkActions` it adds `RestoreBulkAction` and `ForceDeleteBulkAction` beside the `DeleteBulkAction` that is there. `EditProduct::getActions()` gains the same two header actions `EditOrder` has.

Products are referenced by `OrderItem` rows and belong to categories through `product_category_product`, so a soft delete must leave those relations intact, and it does: the row stays, with `deleted_at` set. The agent checks the two widgets that aggregate products, `ProductStats` under the resource and `TopProductsByRevenueChart`, and leaves their queries alone because Eloquent's global scope now excludes trashed products from them, which is the behaviour a shop expects. It notes in the pull request the one place where a hidden product still appears, `OrderItem->product` on an existing order, which is correct for order history.

`tests/Pest.php` binds every test under `tests/Filament` to `RefreshDatabase` and an authenticated user, and `tests/Filament/Resources/Shop/Orders/Pages/ListOrdersTest.php` shows the style: `Livewire::test(ListOrders::class)`, `assertCanSeeTableRecords`, `callAction(TestAction::make(...)->table($record))` and `assertDatabaseHas`. There is no `Products` directory under `tests/Filament/Resources/Shop`, so the agent creates `ListProductsTest.php` and `EditProductTest.php` there: deleting a product hides it from the default table and keeps the row with a `deleted_at`, the trashed filter shows it, restore clears `deleted_at`, force delete removes the row, and the bulk restore works on two records. It runs `php artisan migrate`, `vendor/bin/pint --test`, `composer test:pest` and `composer test:phpstan`, which `tests.yml` runs together as `composer test:pest` after `npm run build`, and pastes the summaries in the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add soft deletes to the Product resource in this Filament 5 panel and open a pull request. Read composer.json, phpunit.xml, tests/Pest.php, app/Models/Shop/Product.php, app/Models/Shop/Order.php, database/migrations/2021_12_13_000014_create_products_table.php, database/migrations/2021_12_13_000016_create_orders_table.php, app/Filament/Resources/Shop/Products/ProductResource.php, Tables/ProductsTable.php, Pages/EditProduct.php, app/Filament/Resources/Shop/Orders/OrderResource.php, Tables/OrdersTable.php, Pages/EditOrder.php and tests/Filament/Resources/Shop/Orders/Pages/ListOrdersTest.php first, and follow the Orders pattern exactly. Add the SoftDeletes trait to Product and a new migration that adds and drops softDeletes on products. In ProductResource add getEloquentQuery removing SoftDeletingScope. In ProductsTable add TrashedFilter first in filters, RestoreAction and ForceDeleteAction after DeleteAction in the record action group, and RestoreBulkAction and ForceDeleteBulkAction beside DeleteBulkAction. In EditProduct add RestoreAction and ForceDeleteAction to getActions. Create tests/Filament/Resources/Shop/Products/Pages/ListProductsTest.php and EditProductTest.php in the style of ListOrdersTest covering delete hiding the row but keeping it with deleted_at, the trashed filter showing it, restore, force delete and bulk restore. Run php artisan migrate, vendor/bin/pint --test, composer test:pest and composer test:phpstan and paste the summaries in the pull request. Do not change the Orders, Customers or HR resources or the existing migrations.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with one new migration, one trait line in `Product.php`, a `getEloquentQuery` method in `ProductResource.php`, a filter and four actions in `ProductsTable.php`, two header actions in `EditProduct.php`, and two new Pest files under `tests/Filament/Resources/Shop/Products/Pages`. The description shows the `composer test:pest` summary with the new tests, the PHPStan result at the project's level, and the Pint check.

The repository's `tests.yml` installs Composer and npm dependencies, builds assets, creates the SQLite database and runs `composer test:pest` on every push and pull request, so a failing migration or a broken Livewire action fails there before review.

A reviewer checks that the migration is additive and reversible, that `withoutGlobalScopes` is on the resource and not on the model, that `ForceDeleteAction` is visible only for trashed records as Filament does by default, and that the stats widgets now exclude trashed products on purpose. Then they decide whether existing hard-deleted products need backfilling, which this change cannot do.

## Run it on every resource that needs an undo

A team bringing every destructive resource in its panels up to the same standard sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own resources and copies the pattern that repository already uses, so a panel on Filament 3 gets the `Tables\Actions` namespaces of that version, and each opens its own pull request.

Send a `request_id` with each body so a retry returns the run already started. With a `webhook_url` on the API key, Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from.

## Why run it on Rush?

Soft deletes touch four layers at once, the model, a migration, the table and the edit page, and missing one produces a panel where delete hides a row that nothing can bring back. In a Rush sandbox the agent runs the migration, drives the Livewire pages through Pest with real records and real actions, and the summaries sit in the run's record beside the commands, so a reviewer sees restore and force delete work rather than trusting the four files line up.

The project has the answer already, in `OrderResource`, `OrdersTable` and `EditOrder`, and the agent follows it file for file, including PHPStan at the project's configured level and Pint's style. The same request body works unchanged on every Filament panel a team runs, and several run at once in their own sandboxes.

## Frequently asked questions

### Why a new migration instead of editing the products table migration?

The 2021 migration has already run wherever this panel is deployed, and Laravel will not run it again. A new migration with `softDeletes()` in `up` and `dropSoftDeletes()` in `down` adds the column everywhere with `php artisan migrate` and can be rolled back; that is the same approach `Order` and `Customer` would have needed had they come later.

### Will deleted products disappear from orders?

No. The `OrderItem` row keeps its `product_id`, and `OrderItem->product` still resolves because Eloquent's soft-delete scope applies to queries on `Product`, not to a row that already exists. The product stops appearing in the Products table, the stats widgets and the revenue chart, which query `Product` directly.

### What is the difference between delete and force delete here?

`DeleteAction` sets `deleted_at` and the row stays; `TrashedFilter` shows it and `RestoreAction` clears the timestamp. `ForceDeleteAction` runs a real `DELETE` and is shown only for trashed records, so a product has to be deleted once before it can be removed for good.

### Does this work without the Orders pattern in my app?

Yes. The agent follows `OrderResource` here because it exists; in a panel with no soft-deleted resource it uses Filament 5's `TrashedFilter`, `RestoreAction`, `ForceDeleteAction` and their bulk versions the same way, and adds the `getEloquentQuery` override the documentation describes.

## More Filament jobs

- [Add Team Invitations to a Filament Panel](https://getrush.ai/run/add-team-invitations/filament/)
- [Add Two-Factor Authentication to a Filament Admin Panel](https://getrush.ai/run/add-two-factor-auth/filament/)
- [Add a User Profile Page to a Filament Panel](https://getrush.ai/run/add-user-profile-page/filament/)

## The same job on other stacks

- [Add Soft Delete to GORM Models](https://getrush.ai/run/add-soft-delete/gorm/)
- [Add Soft Delete to a Koa API](https://getrush.ai/run/add-soft-delete/koa/)
- [Add Soft Delete to a Litestar App](https://getrush.ai/run/add-soft-delete/litestar/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
