|
| 1 | +--- |
| 2 | +title: Delete Records |
| 3 | +description: Delete one or more records using type-safe delete builders. |
| 4 | +category: CRUD |
| 5 | +tags: [delete, deleteMany] |
| 6 | +categoryOrder: 2 |
| 7 | +order: 4 |
| 8 | +--- |
| 9 | + |
| 10 | +# Delete Records |
| 11 | + |
| 12 | +Phi provides two methods for deleting records: |
| 13 | + |
| 14 | +- `Delete` |
| 15 | +- `DeleteMany` |
| 16 | + |
| 17 | +> **Note:** `Delete` always returns the deleted record, regardless of the underlying database. On databases that support `DELETE ... RETURNING`, Phi performs the deletion in a single query. On databases that don't, Phi transparently executes the operation inside a transaction by fetching the record before deleting it, ensuring consistent behavior across all supported SQL dialects. |
| 18 | +
|
| 19 | +--- |
| 20 | + |
| 21 | +## Delete |
| 22 | + |
| 23 | +Deletes a **single record**. |
| 24 | + |
| 25 | +The first predicate **must** uniquely identify a record (for example, `Id.EQ()` or another unique field). Additional predicates may be supplied to further constrain the deletion. |
| 26 | + |
| 27 | +By default, all scalar fields are returned. Use `Select` or `Omit` to customize the returned data. |
| 28 | + |
| 29 | +### Basic |
| 30 | + |
| 31 | +```go |
| 32 | +user, err := db.User.Delete( |
| 33 | + user.Email.EQ("x@y.com"), |
| 34 | +).Exec(ctx) |
| 35 | +``` |
| 36 | + |
| 37 | +### With Additional Predicates |
| 38 | + |
| 39 | +```go |
| 40 | +user, err := db.User.Delete( |
| 41 | + user.Email.EQ("x@y.com"), |
| 42 | + user.Bio.Contains("golang"), |
| 43 | +).Exec(ctx) |
| 44 | +``` |
| 45 | + |
| 46 | +### Returning Selected Fields |
| 47 | + |
| 48 | +```go |
| 49 | +user, err := db.User.Delete( |
| 50 | + user.Id.EQ(id), |
| 51 | +). |
| 52 | + Select(user.Select{ |
| 53 | + Id: true, |
| 54 | + Username: true, |
| 55 | + }). |
| 56 | + Exec(ctx) |
| 57 | +``` |
| 58 | + |
| 59 | +### Omitting Fields |
| 60 | + |
| 61 | +```go |
| 62 | +user, err := db.User.Delete( |
| 63 | + user.Id.EQ(id), |
| 64 | +). |
| 65 | + Omit(user.Omit{ |
| 66 | + Password: true, |
| 67 | + }). |
| 68 | + Exec(ctx) |
| 69 | +``` |
| 70 | + |
| 71 | +### Supported Builder Methods |
| 72 | + |
| 73 | +| Method | Description | |
| 74 | +| ------------------------ | ------------------------------------------------- | |
| 75 | +| [`Select`](/docs/Select) | Return only the selected fields and relations. | |
| 76 | +| [`Omit`](/docs/Omit) | Return all scalar fields except the omitted ones. | |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## DeleteMany |
| 81 | + |
| 82 | +Deletes **all records** matching the supplied predicates. |
| 83 | + |
| 84 | +Unlike `Delete`, no unique predicate is required. |
| 85 | + |
| 86 | +`DeleteMany` returns the number of rows deleted. |
| 87 | + |
| 88 | +### Basic |
| 89 | + |
| 90 | +```go |
| 91 | +deleted, err := db.User.DeleteMany( |
| 92 | + user.Bio.Contains("inactive"), |
| 93 | +).Exec(ctx) |
| 94 | +``` |
| 95 | + |
| 96 | +### Multiple Predicates |
| 97 | + |
| 98 | +```go |
| 99 | +deleted, err := db.User.DeleteMany( |
| 100 | + user.Email.HasSuffix("@example.com"), |
| 101 | + user.LoginCount.LT(5), |
| 102 | +).Exec(ctx) |
| 103 | +``` |
| 104 | + |
| 105 | +### Using Logical Predicates |
| 106 | + |
| 107 | +```go |
| 108 | +deleted, err := db.User.DeleteMany( |
| 109 | + user.Or( |
| 110 | + user.Bio.Contains("spam"), |
| 111 | + user.PhoneNum.HasPrefix("+999"), |
| 112 | + ), |
| 113 | +).Exec(ctx) |
| 114 | +``` |
| 115 | + |
| 116 | +### Return Value |
| 117 | + |
| 118 | +```go |
| 119 | +deleted, err := db.User.DeleteMany( |
| 120 | + user.Bio.Contains("inactive"), |
| 121 | +).Exec(ctx) |
| 122 | + |
| 123 | +fmt.Printf("Deleted %d users\n", deleted) |
| 124 | +``` |
| 125 | + |
| 126 | +### Supported Builder Methods |
| 127 | + |
| 128 | +`DeleteMany` exposes no additional builder methods beyond its predicates. |
| 129 | + |
| 130 | +> **Note:** `DeleteMany` only returns the number of deleted rows. If you need the deleted records themselves, query them before deleting. |
0 commit comments