Raw
1 ---
2 sidebar_label: "softDelete"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `softDelete Method - Data Model Repository Reference`,
9 }
10
11 # {metadata.title}
12
13 The `softDelete` method of a data model repository soft-deletes one or more records of the data model.
14
15 This means that the records are still available in the database, but they are marked as deleted by setting their `deleted_at` property to the deletion date.
16
17 You can later restore the records using the [restore](../restore/page.mdx) method.
18
19 <Note>
20
21 The `softDelete` method requires the data model to have a `deleted_at` property. Data models defined with [model.define](!docs!/learn/fundamentals/data-models) have this property by default, so you don't need to add it manually.
22
23 </Note>
24
25 <Note>
26
27 This reference assumes you've already resolved the data model repository, as explained in the [Data Model Repository Reference](../../page.mdx) documentation.
28
29 </Note>
30
31 ## `softDelete` Parameters
32
33 <TypeList
34 types={[
35 {
36 type: "`string` \\| `string[]` \\| `object`",
37 name: "filters",
38 description: "Can be either the ID of a record to soft-delete, an array of records IDs to soft-delete, or an object of filters to select the records to soft-delete.",
39 required: true,
40 }
41 ]}
42 sectionTitle="softDelete Parameters"
43 />
44 ---
45
46 ## Soft-Delete Record by ID
47
48 <Note>
49
50 As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0), MikroORM dependencies are included in the `@medusajs/framework` package. If you're using an older version of Medusa, change the import statement to `@mikro-orm/knex`.
51
52 </Note>
53
54 ```ts
55 import {
56 InjectTransactionManager,
57 MedusaContext,
58 MedusaService,
59 InjectManager,
60 } from "@medusajs/framework/utils"
61 import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
62 import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
63 import Post from "./models/post"
64
65 class BlogModuleService extends MedusaService({
66 Post,
67 }){
68 // ...
69 @InjectTransactionManager()
70 protected async doSomething_(
71 id: string,
72 @MedusaContext() sharedContext?: Context<EntityManager>
73 ): Promise<any> {
74 const [
75 deletedPosts,
76 deletedEntities,
77 ] = await this.postRepository_.softDelete(id)
78
79 return deletedPosts
80 }
81
82 @InjectManager()
83 async doSomething(
84 id: string,
85 @MedusaContext() sharedContext?: Context<EntityManager>
86 ): Promise<any> {
87 return await this.doSomething_(id, sharedContext)
88 }
89 }
90 ```
91
92 ### Parameters
93
94 The `softDelete` method can accept the ID of a record to soft-delete as a string.
95
96 ### Returns
97
98 The method returns a tuple with two elements:
99
100 - The first element is an array of the soft-deleted records.
101 - The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
102 - This is useful if records from other data models are soft-deleted due to cascading soft-deletes.
103
104 For example:
105
106 ```json title="Example"
107 [
108 [
109 {
110 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
111 "title": "My first post",
112 "content": "This is my first post",
113 "metadata": null,
114 "product_id": null,
115 "product_ids": [
116 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
117 ],
118 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
119 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
120 "created_at": "2025-04-23T12:44:59.751Z",
121 "updated_at": "2025-04-23T12:44:59.751Z",
122 "deleted_at": "2025-04-23T12:45:08.750Z"
123 }
124 ],
125 {
126 "Post": [
127 {
128 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
129 "title": "My first post",
130 "content": "This is my first post",
131 "metadata": null,
132 "product_id": null,
133 "product_ids": [
134 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
135 ],
136 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
137 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
138 "created_at": "2025-04-23T12:44:59.751Z",
139 "updated_at": "2025-04-23T12:44:59.751Z",
140 "deleted_at": "2025-04-23T12:45:08.750Z"
141 }
142 ]
143 }
144 ]
145 ```
146
147 ---
148
149 ## Soft-Delete Multiple Records By IDs
150
151 ```ts
152 import {
153 InjectTransactionManager,
154 MedusaContext,
155 MedusaService,
156 InjectManager,
157 } from "@medusajs/framework/utils"
158 import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
159 import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
160 import Post from "./models/post"
161
162 class BlogModuleService extends MedusaService({
163 Post,
164 }){
165 // ...
166 @InjectTransactionManager()
167 protected async doSomething_(
168 ids: string[],
169 @MedusaContext() sharedContext?: Context<EntityManager>
170 ): Promise<any> {
171 const [
172 deletedPosts,
173 deletedEntities,
174 ] = await this.postRepository_.softDelete(ids)
175
176 return deletedPosts
177 }
178
179 @InjectManager()
180 async doSomething(
181 ids: string[],
182 @MedusaContext() sharedContext?: Context<EntityManager>
183 ): Promise<any> {
184 return await this.doSomething_(ids, sharedContext)
185 }
186 }
187 ```
188
189 ### Parameters
190
191 The `softDelete` method can accept an array of record IDs to soft-delete.
192
193 ### Returns
194
195 The method returns a tuple with two elements:
196
197 - The first element is an array of the soft-deleted records.
198 - The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
199 - This is useful if records from other data models are soft-deleted due to cascading soft-deletes.
200
201 For example:
202
203 ```json title="Example"
204 [
205 [
206 {
207 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
208 "title": "My first post",
209 "content": "This is my first post",
210 "metadata": null,
211 "product_id": null,
212 "product_ids": [
213 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
214 ],
215 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
216 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
217 "created_at": "2025-04-23T12:44:59.751Z",
218 "updated_at": "2025-04-23T12:44:59.751Z",
219 "deleted_at": "2025-04-23T12:45:08.750Z"
220 }
221 ],
222 {
223 "Post": [
224 {
225 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
226 "title": "My first post",
227 "content": "This is my first post",
228 "metadata": null,
229 "product_id": null,
230 "product_ids": [
231 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
232 ],
233 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
234 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
235 "created_at": "2025-04-23T12:44:59.751Z",
236 "updated_at": "2025-04-23T12:44:59.751Z",
237 "deleted_at": "2025-04-23T12:45:08.750Z"
238 }
239 ]
240 }
241 ]
242 ```
243
244 ---
245
246 ## Soft-Delete Records by Filters
247
248 ```ts
249 import {
250 InjectTransactionManager,
251 MedusaContext,
252 MedusaService,
253 InjectManager,
254 } from "@medusajs/framework/utils"
255 import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"
256 import { EntityManager } from "@medusajs/framework/mikro-orm/knex"
257 import Post from "./models/post"
258
259 class BlogModuleService extends MedusaService({
260 Post,
261 }){
262 // ...
263 @InjectTransactionManager()
264 protected async doSomething_(
265 id: string,
266 @MedusaContext() sharedContext?: Context<EntityManager>
267 ): Promise<any> {
268 const [
269 deletedPosts,
270 deletedEntities,
271 ] = await this.postRepository_.softDelete({
272 title: "My Post",
273 })
274
275 return deletedPosts
276 }
277
278 @InjectManager()
279 async doSomething(
280 ids: string[],
281 @MedusaContext() sharedContext?: Context<EntityManager>
282 ): Promise<any> {
283 return await this.doSomething_(ids, sharedContext)
284 }
285 }
286 ```
287
288 ### Parameters
289
290 The `softDelete` method can accept an object of filters to select the records to soft-delete.
291
292 In the example above, you pass an object with the `title` property to soft-delete all records with that title.
293
294 <Note>
295
296 Find examples of different filters in the [Filter Records](../../tips/filtering/page.mdx) documentation.
297
298 </Note>
299
300 ### Returns
301
302 The method returns a tuple with two elements:
303
304 - The first element is an array of the soft-deleted records.
305 - The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
306 - This is useful if records from other data models are soft-deleted due to cascading soft-deletes.
307
308 For example:
309
310 ```json title="Example"
311 [
312 [
313 {
314 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
315 "title": "My first post",
316 "content": "This is my first post",
317 "metadata": null,
318 "product_id": null,
319 "product_ids": [
320 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
321 ],
322 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
323 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
324 "created_at": "2025-04-23T12:44:59.751Z",
325 "updated_at": "2025-04-23T12:44:59.751Z",
326 "deleted_at": "2025-04-23T12:45:08.750Z"
327 }
328 ],
329 {
330 "Post": [
331 {
332 "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",
333 "title": "My first post",
334 "content": "This is my first post",
335 "metadata": null,
336 "product_id": null,
337 "product_ids": [
338 "prod_01JP4M5T5K55P2JN1F17EY7B2T"
339 ],
340 "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",
341 "author": "01JSHAW6YZNKR65TJ242GCMD2S",
342 "created_at": "2025-04-23T12:44:59.751Z",
343 "updated_at": "2025-04-23T12:44:59.751Z",
344 "deleted_at": "2025-04-23T12:45:08.750Z"
345 }
346 ]
347 }
348 ]
349 ```