softDelete Method - Data Model Repository Reference

The softDelete method of a data model repository soft-deletes one or more records of the data model.

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.

You can later restore the records using the restore method.

Note: The softDelete method requires the data model to have a deleted_at property. Data models defined with model.define have this property by default, so you don't need to add it manually.
Note: This reference assumes you've already resolved the data model repository, as explained in the Data Model Repository Reference documentation.

softDelete Parameters#

Loading...

Soft-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.
Code
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 [21      deletedPosts, 22      deletedEntities,23    ] = await this.postRepository_.softDelete(id)24
25    return deletedPosts26  }27
28  @InjectManager()29  async doSomething(30    id: string,31    @MedusaContext() sharedContext?: Context<EntityManager>32  ): Promise<any> {33    return await this.doSomething_(id, sharedContext)34  }35}

Parameters#

The softDelete method can accept the ID of a record to soft-delete as a string.

Returns#

The method returns a tuple with two elements:

  • The first element is an array of the soft-deleted records.
  • The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
    • This is useful if records from other data models are soft-deleted due to cascading soft-deletes.

For example:

Example
1[2  [3    {4      "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5      "title": "My first post",6      "content": "This is my first post",7      "metadata": null,8      "product_id": null,9      "product_ids": [10        "prod_01JP4M5T5K55P2JN1F17EY7B2T"11      ],12      "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13      "author": "01JSHAW6YZNKR65TJ242GCMD2S",14      "created_at": "2025-04-23T12:44:59.751Z",15      "updated_at": "2025-04-23T12:44:59.751Z",16      "deleted_at": "2025-04-23T12:45:08.750Z"17    }18  ],19  {20    "Post": [21      {22        "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23        "title": "My first post",24        "content": "This is my first post",25        "metadata": null,26        "product_id": null,27        "product_ids": [28          "prod_01JP4M5T5K55P2JN1F17EY7B2T"29        ],30        "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31        "author": "01JSHAW6YZNKR65TJ242GCMD2S",32        "created_at": "2025-04-23T12:44:59.751Z",33        "updated_at": "2025-04-23T12:44:59.751Z",34        "deleted_at": "2025-04-23T12:45:08.750Z"35      }36    ]37  }38]

Soft-Delete Multiple Records By IDs#

Code
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    ids: string[],18    @MedusaContext() sharedContext?: Context<EntityManager>19  ): Promise<any> {20    const [21      deletedPosts, 22      deletedEntities,23    ] = await this.postRepository_.softDelete(ids)24
25    return deletedPosts26  }27
28  @InjectManager()29  async doSomething(30    ids: string[],31    @MedusaContext() sharedContext?: Context<EntityManager>32  ): Promise<any> {33    return await this.doSomething_(ids, sharedContext)34  }35}

Parameters#

The softDelete method can accept an array of record IDs to soft-delete.

Returns#

The method returns a tuple with two elements:

  • The first element is an array of the soft-deleted records.
  • The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
    • This is useful if records from other data models are soft-deleted due to cascading soft-deletes.

For example:

Example
1[2  [3    {4      "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5      "title": "My first post",6      "content": "This is my first post",7      "metadata": null,8      "product_id": null,9      "product_ids": [10        "prod_01JP4M5T5K55P2JN1F17EY7B2T"11      ],12      "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13      "author": "01JSHAW6YZNKR65TJ242GCMD2S",14      "created_at": "2025-04-23T12:44:59.751Z",15      "updated_at": "2025-04-23T12:44:59.751Z",16      "deleted_at": "2025-04-23T12:45:08.750Z"17    }18  ],19  {20    "Post": [21      {22        "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23        "title": "My first post",24        "content": "This is my first post",25        "metadata": null,26        "product_id": null,27        "product_ids": [28          "prod_01JP4M5T5K55P2JN1F17EY7B2T"29        ],30        "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31        "author": "01JSHAW6YZNKR65TJ242GCMD2S",32        "created_at": "2025-04-23T12:44:59.751Z",33        "updated_at": "2025-04-23T12:44:59.751Z",34        "deleted_at": "2025-04-23T12:45:08.750Z"35      }36    ]37  }38]

Soft-Delete Records by Filters#

Code
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 [21      deletedPosts, 22      deletedEntities,23    ] = await this.postRepository_.softDelete({24      title: "My Post",25    })26
27    return deletedPosts28  }29
30  @InjectManager()31  async doSomething(32    ids: string[],33    @MedusaContext() sharedContext?: Context<EntityManager>34  ): Promise<any> {35    return await this.doSomething_(ids, sharedContext)36  }37}

Parameters#

The softDelete method can accept an object of filters to select the records to soft-delete.

In the example above, you pass an object with the title property to soft-delete all records with that title.

Note: Find examples of different filters in the Filter Records documentation.

Returns#

The method returns a tuple with two elements:

  • The first element is an array of the soft-deleted records.
  • The second element is an object whose keys are the names of deleted data models, and values an array of deleted record objects.
    • This is useful if records from other data models are soft-deleted due to cascading soft-deletes.

For example:

Example
1[2  [3    {4      "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",5      "title": "My first post",6      "content": "This is my first post",7      "metadata": null,8      "product_id": null,9      "product_ids": [10        "prod_01JP4M5T5K55P2JN1F17EY7B2T"11      ],12      "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",13      "author": "01JSHAW6YZNKR65TJ242GCMD2S",14      "created_at": "2025-04-23T12:44:59.751Z",15      "updated_at": "2025-04-23T12:44:59.751Z",16      "deleted_at": "2025-04-23T12:45:08.750Z"17    }18  ],19  {20    "Post": [21      {22        "id": "01JSHAW6Z7KW4X6E8MFPGNEKHC",23        "title": "My first post",24        "content": "This is my first post",25        "metadata": null,26        "product_id": null,27        "product_ids": [28          "prod_01JP4M5T5K55P2JN1F17EY7B2T"29        ],30        "author_id": "01JSHAW6YZNKR65TJ242GCMD2S",31        "author": "01JSHAW6YZNKR65TJ242GCMD2S",32        "created_at": "2025-04-23T12:44:59.751Z",33        "updated_at": "2025-04-23T12:44:59.751Z",34        "deleted_at": "2025-04-23T12:45:08.750Z"35      }36    ]37  }38]
Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵