delete Method - Data Model Repository Reference
The delete method of a data model repository deletes one or more records of the data model.
Note: This reference assumes you've already resolved the data model repository, as explained in the Data Model Repository Reference documentation.
delete Parameters#
Loading...
Delete Record by ID#
Note: As of Medusa 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.1import { 2 InjectTransactionManager,3 MedusaContext,4 MedusaService,5 InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10 11class BlogModuleService extends MedusaService({12 Post,13}){14 // ...15 @InjectTransactionManager()16 protected async doSomething_(17 id: string,18 @MedusaContext() sharedContext?: Context<EntityManager>19 ): Promise<any> {20 const deletedIds = await this.postRepository_.delete({21 id,22 })23 24 return deletedIds25 }26 27 @InjectManager()28 async doSomething(29 id: string,30 @MedusaContext() sharedContext?: Context<EntityManager>31 ): Promise<any> {32 return await this.doSomething_(id, sharedContext)33 }34}
Parameters#
The delete method accepts an object of filters to select the records to delete.
In the example above, you pass an object with the property to delete a record by its ID.
Returns#
The method returns an array of strings, each representing the ID of a deleted record.
Delete Records by Filters#
1import { 2 InjectTransactionManager,3 MedusaContext,4 MedusaService,5 InjectManager,6} from "@medusajs/framework/utils"7import { Context, InferTypeOf, DAL } from "@medusajs/framework/types"8import { EntityManager } from "@medusajs/framework/mikro-orm/knex"9import Post from "./models/post"10 11class BlogModuleService extends MedusaService({12 Post,13}){14 // ...15 @InjectTransactionManager()16 protected async doSomething_(17 id: string,18 @MedusaContext() sharedContext?: Context<EntityManager>19 ): Promise<any> {20 const deletedIds = await this.postRepository_.delete({21 title: "My Post",22 })23 24 return deletedIds25 }26 27 @InjectManager()28 async doSomething(29 id: string,30 @MedusaContext() sharedContext?: Context<EntityManager>31 ): Promise<any> {32 return await this.doSomething_(id, sharedContext)33 }34}
Parameters#
Since the delete method accepts an object of filters, you can pass any property of the data model as a filter.
In the example above, you pass an object with the title property to delete all records with that title.
Note: Find examples of different filters in the Filter Records documentation.
Returns#
The method returns an array of strings, each representing the ID of a deleted record.
Was this page helpful?