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