Delete
Delete by key, struct, or condition; soft-delete rewriting, Unscoped, and the global-delete guard.
// Delete removes matching rows. A WHERE clause (or value with a primary
// key) is required unless AllowGlobalUpdate is set.Delete(value, conds...) runs the delete callback chain. The statement
depends on whether the model carries a soft-delete field.
Ways to delete
By primary key value — a scalar condition binds the single primary key
(composite-key models reject scalars with ErrInvalidValue; see
composite primary keys):
// given a fetched slice `all`, delete the first row by its key:
if err := db.Delete(&softUser{}, all[0].ID).Error; err != nil {
panic(err)
}By struct condition — a model value with non-zero fields becomes the WHERE clause:
del := db.Delete(&crudUser{Name: "carol"})
// DELETE FROM "crud_users" WHERE "name" = 'carol'By explicit condition — chain Where and pass a model (or nil) as
the value:
globalDelete := db.Model(&crudUser{}).Delete(nil)
// no conditions anywhere: ErrMissingWhereClause
db.Model(&crudUser{}).Where("age < ?", 18).Delete(nil)No slice-of-keys form
Unlike GORM, Delete(&User{}, []int64{1, 2, 3}) is not supported — slice
conditions are not one of the accepted forms (scalar, SQL string, map,
struct), so the statement fails instead of guessing. Delete in a loop, or
build the IN list into the SQL string yourself:
db.Model(&User{}).Where("id IN (1, 2, 3)").Delete(nil).
The global-delete guard
A delete with no conditions anywhere — no WHERE, no non-zero model fields
— is rejected with fusion.ErrMissingWhereClause. The guard runs
before the soft-delete rewrite, so the injected live-row filter cannot
satisfy it and turn a mistake into a global soft delete:
// TestAuditGlobalSoftDeleteRequiresWhere: Delete without any caller
// conditions on a soft-deletable model must fail with ErrMissingWhereClause
// instead of soft-deleting every live row.Opt in with Session(&fusion.Session{AllowGlobalUpdate: true}), as with
updates.
Soft delete: DELETE becomes UPDATE
On a model with a soft-delete field (embed fusion.DeletedAt, or tag a
field fusion:"softdelete"), Delete does not remove the row — it
rewrites the statement into an update of that column, and appends the
live-row filter so only live rows are affected:
// rewriteSoftDelete converts DELETE into an UPDATE of the soft-delete
// column. It reports whether the rewrite happened.UPDATE "soft_users" SET "deleted_at" = '2026-11-28 10:00:00'
WHERE id = 1 AND "deleted_at" IS NULLEvery subsequent query on the model adds AND "deleted_at" IS NULL
automatically, and the OR-precedence is regrouped so deleted rows can
never leak back in. See soft delete for the full
model surface, including Restore and custom zero/delete values.
Unscoped — hard delete
Unscoped() disables both the DELETE rewrite and the query filter for
that statement:
// Unscoped disables soft-delete filtering and DELETE rewriting for the
// statement, including relation preloads issued from it.// `all[0]` is a previously fetched row; Unscoped still finds it:
var hidden softUser
if err := db.Unscoped().First(&hidden, all[0].ID).Error; err != nil {
panic(err)
}The same chainable switches a soft-delete model's Delete back to a real
DELETE FROM.
What runs
The delete chain:
// The delete chain, in order: begin_tx, before_delete,
// delete_before_associations, delete, after_delete, commit_or_rollback.BeforeDelete/AfterDelete hooks run around the statement, and the
association step removes dependent rows and junction entries before the
root delete — see
association callbacks.