Skip to main content

Soft deletes

A soft delete sets a timestamp on the row and hides it from queries. Add a field of type ant.DeletedAt to a model to turn it on.

Turn it on​

type Note struct {
ant.Base
Text string
DeletedAt ant.DeletedAt
}

var Notes = ant.Fields[Note]()

The column is a nullable timestamp. NULL means the row is live.

Delete a row​

Delete, DeleteByID and ant.Remove on a model with DeletedAt mark the row instead of removing it:

n, err := db.Notes.Where(ant.Eq(&Notes.Text, "draft")).Delete(ctx)
// UPDATE "notes" SET "deleted_at" = $1, "updated_at" = $2
// WHERE "notes"."text" = $3 AND "notes"."deleted_at" IS NULL
err := db.Notes.DeleteByID(ctx, id)
// UPDATE "notes" SET "deleted_at" = $1, "updated_at" = $2
// WHERE "notes"."id" = $3 AND "notes"."deleted_at" IS NULL

Deleting an already marked row keeps the first timestamp. A delete is a write, so it stamps UpdatedAt, and on a model with a version it moves the version on, so a copy loaded before cannot save over the deleted row.

Read live rows​

Every query skips marked rows: Find, Count, Include, Has, joins and subqueries.

all, err := db.Notes.All(ctx)
// SELECT ... FROM "notes" WHERE "notes"."deleted_at" IS NULL

Read deleted rows​

WithDeleted includes marked rows. OnlyDeleted returns only those.

every, err := db.Notes.WithDeleted().All(ctx)
// SELECT ... FROM "notes"

gone, err := db.Notes.OnlyDeleted().All(ctx)
// SELECT ... FROM "notes" WHERE "notes"."deleted_at" IS NOT NULL

Restore a row​

Restore clears the mark and returns how many rows it changed.

n, err := db.Notes.Where(ant.Eq(&Notes.Text, "draft")).Restore(ctx)
// UPDATE "notes" SET "deleted_at" = $1, "updated_at" = $2
// WHERE "notes"."text" = $3 AND "notes"."deleted_at" IS NOT NULL
// -- $1 = NULL

It stamps UpdatedAt and moves a version on, as a delete does. On a model without a DeletedAt field, Restore returns ant.ErrNotSoftDeleted.

Delete for good​

ForceDelete removes rows for real. Add WithDeleted to reach rows that are already marked.

n, err := db.Notes.WithDeleted().Where(ant.Eq(&Notes.Text, "draft")).ForceDelete(ctx)
// DELETE FROM "notes" WHERE "notes"."text" = $1

Empty the trash​

Prune removes matching rows for good, marked or not, in batches of 1,000, so no single transaction runs long. This purges rows deleted more than 30 days ago:

cutoff := time.Now().AddDate(0, 0, -30)

removed, err := db.Notes.Prune(ctx, ant.Lt(&Notes.DeletedAt.V, cutoff))
// DELETE FROM "notes" WHERE "notes"."id" IN (
// SELECT "notes"."id" FROM "notes" WHERE "notes"."deleted_at" < $1 LIMIT $2
// )
// -- $2 = 1000

Filter by when a row was deleted​

&Notes.DeletedAt.V names the column as a time.

stale, err := db.Notes.OnlyDeleted().Where(ant.Lt(&Notes.DeletedAt.V, cutoff)).Count(ctx)
// SELECT COUNT(*) FROM "notes"
// WHERE "notes"."deleted_at" IS NOT NULL AND "notes"."deleted_at" < $1

ant binds times in UTC on SQLite and SQL Server, whose columns keep no time zone, so DeletedAt compares correctly across server zones. See databases.

Reuse a unique value​

A plain unique constraint also covers marked rows, so their emails can't be reused. Index only live rows:

func (User) Configure(b *ant.ModelBuilder[User]) {
b.Index(&Users.Email).Unique().Where("deleted_at IS NULL")
}