|
1
|
--- |
|
2
|
sidebar_label: "delete" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `delete Method - Data Model Repository Reference`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
The `delete` method of a data model repository deletes one or more records of the data model. |
|
14
|
|
|
15
|
<Note> |
|
16
|
|
|
17
|
This reference assumes you've already resolved the data model repository, as explained in the [Data Model Repository Reference](../../page.mdx) documentation. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
## `delete` Parameters |
|
22
|
|
|
23
|
<TypeList |
|
24
|
types={[ |
|
25
|
{ |
|
26
|
type: "`object`", |
|
27
|
name: "filters", |
|
28
|
description: "The filters to select the records to delete. The keys are the property names of the data model, and the values are the values to filter by.", |
|
29
|
required: true, |
|
30
|
} |
|
31
|
]} |
|
32
|
sectionTitle="delete Parameters" |
|
33
|
/> |
|
34
|
--- |
|
35
|
|
|
36
|
## Delete Record by ID |
|
37
|
|
|
38
|
<Note> |
|
39
|
|
|
40
|
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`. |
|
41
|
|
|
42
|
</Note> |
|
43
|
|
|
44
|
```ts |
|
45
|
import { |
|
46
|
InjectTransactionManager, |
|
47
|
MedusaContext, |
|
48
|
MedusaService, |
|
49
|
InjectManager, |
|
50
|
} from "@medusajs/framework/utils" |
|
51
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
52
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
53
|
import Post from "./models/post" |
|
54
|
|
|
55
|
class BlogModuleService extends MedusaService({ |
|
56
|
Post, |
|
57
|
}){ |
|
58
|
// ... |
|
59
|
@InjectTransactionManager() |
|
60
|
protected async doSomething_( |
|
61
|
id: string, |
|
62
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
63
|
): Promise<any> { |
|
64
|
const deletedIds = await this.postRepository_.delete({ |
|
65
|
id, |
|
66
|
}) |
|
67
|
|
|
68
|
return deletedIds |
|
69
|
} |
|
70
|
|
|
71
|
@InjectManager() |
|
72
|
async doSomething( |
|
73
|
id: string, |
|
74
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
75
|
): Promise<any> { |
|
76
|
return await this.doSomething_(id, sharedContext) |
|
77
|
} |
|
78
|
} |
|
79
|
``` |
|
80
|
|
|
81
|
### Parameters |
|
82
|
|
|
83
|
The `delete` method accepts an object of filters to select the records to delete. |
|
84
|
|
|
85
|
In the example above, you pass an object with the `id` property to delete a record by its ID. |
|
86
|
|
|
87
|
### Returns |
|
88
|
|
|
89
|
The method returns an array of strings, each representing the ID of a deleted record. |
|
90
|
|
|
91
|
--- |
|
92
|
|
|
93
|
## Delete Records by Filters |
|
94
|
|
|
95
|
```ts |
|
96
|
import { |
|
97
|
InjectTransactionManager, |
|
98
|
MedusaContext, |
|
99
|
MedusaService, |
|
100
|
InjectManager, |
|
101
|
} from "@medusajs/framework/utils" |
|
102
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
103
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
104
|
import Post from "./models/post" |
|
105
|
|
|
106
|
class BlogModuleService extends MedusaService({ |
|
107
|
Post, |
|
108
|
}){ |
|
109
|
// ... |
|
110
|
@InjectTransactionManager() |
|
111
|
protected async doSomething_( |
|
112
|
id: string, |
|
113
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
114
|
): Promise<any> { |
|
115
|
const deletedIds = await this.postRepository_.delete({ |
|
116
|
title: "My Post", |
|
117
|
}) |
|
118
|
|
|
119
|
return deletedIds |
|
120
|
} |
|
121
|
|
|
122
|
@InjectManager() |
|
123
|
async doSomething( |
|
124
|
id: string, |
|
125
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
126
|
): Promise<any> { |
|
127
|
return await this.doSomething_(id, sharedContext) |
|
128
|
} |
|
129
|
} |
|
130
|
``` |
|
131
|
|
|
132
|
### Parameters |
|
133
|
|
|
134
|
Since the `delete` method accepts an object of filters, you can pass any property of the data model as a filter. |
|
135
|
|
|
136
|
In the example above, you pass an object with the `title` property to delete all records with that title. |
|
137
|
|
|
138
|
<Note> |
|
139
|
|
|
140
|
Find examples of different filters in the [Filter Records](../../tips/filtering/page.mdx) documentation. |
|
141
|
|
|
142
|
</Note> |
|
143
|
|
|
144
|
### Returns |
|
145
|
|
|
146
|
The method returns an array of strings, each representing the ID of a deleted record. |