{"id":14975521,"url":"https://github.com/passioninfinite/mongoosejs-soft-delete","last_synced_at":"2025-10-27T14:30:25.638Z","repository":{"id":33613181,"uuid":"159085213","full_name":"passionInfinite/mongoosejs-soft-delete","owner":"passionInfinite","description":"Soft Delete plugin for mongoose.","archived":false,"fork":false,"pushed_at":"2023-04-29T22:40:55.000Z","size":64,"stargazers_count":24,"open_issues_count":9,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T05:51:12.509Z","etag":null,"topics":["delete","deletemany","deleteone","mongodb","mongoose","mongoose-plugin","mongoosejs","mongoosejs-soft-delete","plugin","removeone","schema","soft","soft-deletes"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/passionInfinite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-25T23:40:09.000Z","updated_at":"2023-01-26T12:51:35.000Z","dependencies_parsed_at":"2024-06-19T03:17:10.730Z","dependency_job_id":"ea70b19c-c68f-448a-91f8-d851ba9c2f22","html_url":"https://github.com/passionInfinite/mongoosejs-soft-delete","commit_stats":{"total_commits":27,"total_committers":3,"mean_commits":9.0,"dds":"0.11111111111111116","last_synced_commit":"9d1e8838cfbd0927737bb10362305c63fd3ed042"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/passionInfinite%2Fmongoosejs-soft-delete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/passionInfinite%2Fmongoosejs-soft-delete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/passionInfinite%2Fmongoosejs-soft-delete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/passionInfinite%2Fmongoosejs-soft-delete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/passionInfinite","download_url":"https://codeload.github.com/passionInfinite/mongoosejs-soft-delete/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238508433,"owners_count":19484133,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["delete","deletemany","deleteone","mongodb","mongoose","mongoose-plugin","mongoosejs","mongoosejs-soft-delete","plugin","removeone","schema","soft","soft-deletes"],"created_at":"2024-09-24T13:52:09.488Z","updated_at":"2025-10-27T14:30:25.127Z","avatar_url":"https://github.com/passionInfinite.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoosejs Soft Delete Plugin\n[![Build Status](https://travis-ci.com/passionInfinite/mongoosejs-soft-delete.svg?branch=master)](https://travis-ci.com/passionInfinite/mongoosejs-soft-delete)\n[![GitHub license](https://img.shields.io/github/license/passionInfinite/mongoosejs-soft-delete.svg)](https://github.com/passionInfinite/mongoosejs-soft-delete/blob/master/LICENSE)\n\nA lightweight plugin that enables the soft delete functionality for documents in MongoDB.\nThis code is based on [mongoose-delete](https://github.com/dsanel/mongoose-delete).\n\n### Usage Guidelines\n\n* [Basic Usage](https://github.com/passionInfinite/mongoosejs-soft-delete#basic-usage)\n* [Advance Usage](https://github.com/passionInfinite/mongoosejs-soft-delete#advance-usage)\n\n### How it Works\n    \n* [Functions performing with Mongoose](https://github.com/passionInfinite/mongoosejs-soft-delete#how-it-works)\n\n### Functions Support\n\n* [Find and update functions](https://github.com/passionInfinite/mongoosejs-soft-delete#functions-available)\n* [Hard and soft delete functions](https://github.com/passionInfinite/mongoosejs-soft-delete)\n\n### Handling Hooks\n\n* [removeMany and removeOne hooks](https://github.com/passionInfinite/mongoosejs-soft-delete#hooks)\n\n# Installation\nInstall using [npm](https://www.npmjs.com/package/mongoosejs-soft-delete)\n\n```npm install mongoosejs-soft-delete```\n\n# Usage\nYou can use this plugin with or without passing the options to the plugin.\n\n### Basic Usage\n\n```\nlet softDelete = require('mongoosejs-soft-delete');\nlet SampleSchema = new mongoose.Schema({\n    comment: String\n})\nSampleSchema.plugin(softDelete) //Without passing any parameters.\n\nlet Sample = mongoose.model('Sample', SampleSchema);\nlet document = new Sample({\n    comment: \"This plugin is awesome.\"\n});\n   \nlet sampleId = mongoose.Types.ObjectId(\"507f1f77bcf86cd799439011\");\n\n//This will do soft delete for one document.\nSample.removeOne({ _id: sampleId }, function (err, res) {});\nSample.findByIdDeleted(sampleId, function(err, doc) {\n    //doc.deletedAt will be current timestamp\n    //doc.deleted will be true\n})\n\n//This will do soft delete for multiple document.\nSample.removeMany({ _id: sampleId }, function (err, res) {});\n\n// If you want to perform hard delete.\n//For deleting one document.\nSample.deleteOne({ _id: sampleId }, callback);\n\n//For deleting multiple documents.\nSample.deleteMany(conditions, callback);\n\n```\n\nBy default, basic usage will set the `deleted` and `deletedAt` field.\nThe type of `deleted` field will be `Boolean` whereas `deletedAt` will have the current timestamp.\n\n### Advance Usage\n\nIf you want to change the default behaviour of the plugin. For example, instead\nof deletedAt you want to have custom field and the value of that custom field should be\na custom function then you can use this second option.\n\n```\nlet softDelete = require('mongoosejs-soft-delete');\nlet SampleSchema = new mongoose.Schema({\n    comment: String\n})\n\n//To define our on custom field pass two params: index and define that index.\n\nfunction getCustom() {\n    return true;\n}\n\nSampleSchema.plugin(softDelete, { index: 'custom', custom: getCustom()) \n\nlet Sample = mongoose.model('Sample', SampleSchema);\nlet document = new Sample({\n    comment: \"This plugin is awesome.\"\n});\n   \nlet sampleId = mongoose.Types.ObjectId(\"507f1f77bcf86cd799439011\");\n\n//This will do soft delete for one document.\nSample.removeOne({ _id: sampleId }, function (err, res) {});\nSample.findByIdDeleted(sampleId, function(err, doc) {\n    //doc.custom will be true\n    //doc.deleted will be true\n})\n\n//This will do soft delete for multiple document.\nSample.removeMany({ _id: sampleId }, function (err, res) {});\n\n// If you want to perform hard delete.\n//For deleting one document.\nSample.deleteOne({ _id: sampleId }, callback);\n\n//For deleting multiple documents.\nSample.deleteMany(conditions, callback);\n\n``` \n\n**If you are passing your own custom function make sure it \nhas some return type. Any return type except the `function`.**\n\nAlso you can directly assign certain value the index field value.\n\n```\n    SampleSchema.plugin(softDelete, { index: 'custom', custom: true });\n```\n# How it works\n\n**We haven't override the mongoose deleteOne and deleteMany function\nso it will perform hard delete (not soft delete).**\n\nBelow table shows the functions that will do soft delete.\n\n| Mongoose Function |  Under Hood Performs |\n|:-----------------:|:--------------------:|\n|     findById      |    findOne           |\n|     findOneAndRemove | findOneAndUpdate  |\n|    findOneAndDelete | findOneAndUpdate   |\n|    findByIdAndRemove | findByIdAndUpdate |\n|    findByIdAndDelete| findByIdAndUpdate  |\n|  removeOne         |  updateOne          |\n|  removeMany        |  updateMany         |\n\n\n# Functions Support\n\n\n\nFor the following method we have methods to query the non-soft-deleted documents,\nsoft deleted (suffix: Deleted) document and both of the documents(suffix: withDeleted).\n\n| Non-Deleted Documents | Soft Deleted Documents      | All Documents                 |\n|:---------------------:|:---------------------------:|:-----------------------------:|\n|find                   |    findDeleted              |  findWithDeleted              |\n|findByIdAndUpdate      |    findByIdAndUpdateDeleted |  findByIdAndWithUpdateDeleted |\n|findOne                |    findOneDeleted           |  findOneWithDeleted           |\n|findOneAndUpdate       |    findOneAndUpdateDeleted  |  findOneAndUpdateWithDeleted  |\n|replaceOne             |    replaceOneDeleted        |  replaceOneWithDeleted        |\n|updateMany             |    updateManyDeleted        |  updateManyWithDeleted        |\n|updateOne              |    updateOneDeleted         |  updateOneWithDeleted         |\n\n### Hard and soft delete functions\n\nThe following method performs delete feature.\n\n| Function              | Type       |      \n|:---------------------:|:----------:|\n| deleteOne             | Hard Delete|\n| deleteMany            | Hard Delete|\n| removeOne             | Soft Delete|\n| removeMany            | Soft Delete|\n\n\n### Hooks\n\nUsing mongoose hooks for soft delete.\nThere is always a case like you want to remove the related documents on soft delete\nthen you can always use the below hook.\n\n**Note: This hook is only useful if you call removeOne and removeMany and\n always passing the required constraint for fetching relational data**\n\n```\n   let softDelete = require('mongoosejs-soft-delete');\n   let SampleSchema = new mongoose.Schema({\n       comment: String\n   })\n   \n   SampleSchema.plugin(softDelete);\n   \n   //This hook will run if you use Model.removeOne() method.\n   SampleSchema.post('updateOne', async function() {\n        if (this._update.deleted) {\n            //this doc is deleted so you can easily perform clean up here.\n        }\n   });\n   \n   \n   //This hook will run if you use Model.removeMany() method.\n   SampleSchema.post('updateMany', async function() {\n        if (this._update.deleted) {\n            //do your clean up work.\n        }\n   });\n \n```\n\nAlso, you can use the `_conditions` to the query parameters\nthat you passed while calling the removeMany or removeOne method.\n\n \n \n# License\nCopyright (c) 2018 [Hardik Patel](http://github.com/passioninfinite) and [Parth Patel](http://github.com/parth7676)\nCode released under [MIT](https://github.com/passionInfinite/mongoosejs-soft-delete/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpassioninfinite%2Fmongoosejs-soft-delete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpassioninfinite%2Fmongoosejs-soft-delete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpassioninfinite%2Fmongoosejs-soft-delete/lists"}