Skip to content

Entities 0.2.0: A trash for records, with restore and delete forever

Entities 0.2.0 adds a trash for entity records. Deleting a record now moves it to the trash, where it can be restored or deleted for good, so records that other parts of your app point at are not lost by mistake.

The trash in the data navigator

The data navigator gets a Trash filter with a count of trashed records. Records get a Move to trash button, and each trashed record has Restore from trash and Delete forever buttons. The bulk action bar changes with the view: on the normal views it offers Archive, Restore and Trash, and in the trash it offers Restore and Delete forever. Every bulk button now shows that it is working while the action runs.

Trashed records are left out of the normal lists, searches and counts, and out of mirror exports, so they do not come back on the next export. A trashed record keeps its slug, so a new record cannot take it while the old one is in the trash. A record restored from the trash returns as a draft. Toggling a record’s status skips the trashed state; restoring is the only way out.

The message after trashing a record says to restore it within 90 days, but this release has nothing that empties the trash on its own.

When other tables point at a record

Your app may keep foreign keys to entity records, for example an order’s status. Deleting such a record forever used to fail with a server error. It now returns a clear message instead. An app can also list, in the :reverse_references setting of :phoenix_kit_entities, functions that count the rows using a record, to show “used by” hints; they do not block a delete.

Other changes

The public form’s check for private network addresses no longer accepts values like 123abc, and the sitemap source now logs the errors it used to hide.

Upgrading

No migration: the trash is a new record status. Code that calls delete/2 or bulk_delete/2 and matches only {:ok, _} and a changeset error should also handle {:error, :referenced_by_external}. Bulk delete in the navigator now moves records to the trash; deleting forever happens from the trash view.

Full release notes: Entities 0.2.0

0.2.0 · 4 May 2026

Added

  • Soft-delete for EntityData (issue #12) — keeps rows alive when parent apps hold FK references (e.g. orders.status_uuid → phoenix_kit_entity_data.uuid). New status sentinel "trashed" joins the existing {draft, published, archived} set; no migration required.
  • New public API on PhoenixKitEntities.EntityData: trash/2, restore_from_trash/2, bulk_trash/2, bulk_restore_from_trash/2, list_trashed_by_entity/2, trashed_count/1.
  • count_external_references/1 and count_external_references/2 — reads Application.get_env(:phoenix_kit_entities, :reverse_references, []) (a list of {entity_name, count_fn} tuples) so parent apps can surface “used by N rows” hints. The 2-arity form takes a pre-loaded entity to skip the per-call preload when rendering many records. Informational only — not a delete-blocker.
  • Three new error atoms in PhoenixKitEntities.Errors: :already_trashed, :not_trashed, :referenced_by_external with localized messages.
  • DataNavigator admin UX: Trash filter view with count badge, per-row Restore-from-trash + Delete-forever buttons on trashed records, bulk-action bar branches by view (Archive/Restore/Trash on default views; Restore/Delete-forever on the Trash view).
  • 130 net new tests — entity_data_trash_test.exs (49 tests) builds transient _trash_test_parent tables that mirror issue #12’s exact NOT NULL REFERENCES … ON DELETE RESTRICT shape, exercising FK-violation paths against a real parent FK. New describe block in data_navigator_live_test.exs (18 tests) covers all event handlers + authorization + bulk-bar branching.
  • AGENTS.md gained a “Soft-delete (trash) for EntityData” section documenting the parent-app FK motivation, public API, default-list filtering rules, slug uniqueness rationale, and the :reverse_references config hook.

Changed

  • delete/2 and bulk_delete/2 now catch Ecto.ConstraintError (foreign-key) and Postgrex.Error (SQLSTATE 23503 / 23502) and return {:error, :referenced_by_external} instead of raising. The admin UI flashes a friendly message rather than a 500. Soft return-shape change — callers exhaustively pattern-matching {:ok, _} | {:error, %Ecto.Changeset{}} should add a clause for the new atom.
  • Default-list queries (list_all/1, list_by_entity/2, search_by_title/3, count_by_entity/2) exclude trashed records by default. Pass include_trashed: true to opt in (admin trash views, reverse-reference checks). Mirror exporter inherits this exclusion — trashed records won’t resurrect on re-export.
  • get_data_stats/1 now returns trashed_records separately; total_records reflects the visible (non-trashed) count.
  • get_by_slug/2 deliberately surfaces trashed rows so slug uniqueness is preserved across the trash bin.
  • DataNavigator bulk Delete repurposed → soft-trash; permanent delete is a separate action available only from the Trash filter view.
  • toggle_status cycle skips trashed (Restore is the only escape).
  • phx-disable-with added to all 8 bulk-action buttons (3 from soft-delete additions + 5 pre-existing oversights).
  • entity_form_controller.ex private_or_local_ip?/1 swapped from String.to_integer + rescue _ to Integer.parse/1 with explicit with/else chain — pins {int, ""} so "123abc" no longer slips through as 123.
  • sitemap_source.ex sub_sitemaps/1 rescue _ -> nil now logs the error inspect, matching the canonical pattern at the other two rescues in the file.
  • sitemap_source.ex enabled?/0 gained catch :exit, _ -> false to match the boot-resilience shape from phoenix_kit_entities.ex (sandbox-shutdown signals).
  • @spec backfill on list_trashed_by_entity/2 and trashed_count/1.

Entities 0.2.0. Install or update: {:phoenix_kit_entities, "~> 0.2"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.