Skip to main content
Version: v2.x.x

Soft Delete

Soft Deleting​

In addition to actually removing records from your database, Mongoloquent can also soft delete models. When models are soft deleted, they are not actually removed from your database. Instead, a isDeleted and deletedAt attribute is set on the model indicating the date and time at which the model was "deleted". To enable soft deletes for a model, add the static property softDelete to the model.

import { Mongoloquent } from "mongoloquent";

class User extends Mongoloquent {
static collection = "users";
static softDelete = true;
}

The static property softDelete will automatically add the isDeleted and deletedAt attributes to your model.

Restoring Soft Deleted Models​

Sometimes you may wish to "un-delete" a soft deleted model. To restore a soft deleted model, you may call the restore method on a model. The restore method will set the model's deletedAt column to null and isDeleted column to false.

import User from "./yourPath/User";

await User.withTrashed().restore();

You may also combine the restore method with query methods to restore multiple models/data.

import User from "./yourPath/User";

await User.withTrashed().where("age", ">=", 50).restore();

Permanently Deleting Models​

Sometimes you may need to truly remove a model from your database. You may use the forceDelete method to permanently remove a soft deleted model from the database collection.

import User from "./yourPath/User";

await User.forceDelete();

You may also combine the forceDelete method with query methods to permanently delete some specific soft deleted models/data.

import User from "./yourPath/User";

await User.where("age", 50).forceDelete();

Querying Soft Deleted Models​

Including Soft Deleted Models​

As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to be included in a query's results by calling the withTrashed method on the query.

import User from "./yourPath/User";

const users = await User.withTrashed().where("age", ">=" 50).get()

Retrieving Only Soft Deleted Models​

The onlyTrashed method will retrieve only soft deleted models.

import User from "./yourPath/User";

const users = await User.onlyTrashed().where("age", "gte" 50).get()

Support us​

Mongoloquent is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.

Sponsors​

_

Partners​