Documentation chapters
You're looking at legacy documentation.
New multi-language documentation available at
ORM / Plugin

Soft Delete

The Soft Delete plugin allows to keep database records hidden without actually deleting them. When a record is deleted, it only is marked as deleted and not really deleted. All queries automatically filter on that deleted property, so to the user it feels like they are actually deleted.

To use the plugin, you have to instantiated the SoftDelete class and activate it for each entity.

import { entity, PrimaryKey, AutoIncrement } from '@deepkit/type';
import { SoftDelete } from '@deepkit/orm';'user')
class User {
    id: number & PrimaryKey & AutoIncrement = 0;
    created: Date = new Date;

    // this field is used as indicator whether the record is deleted.
    deletedAt?: Date;

    // this field is optional and can be used to track who/what deleted the record.
    deletedBy?: string;

        public name: string
    ) {

const softDelete = new SoftDelete(database);

//or disable again


To soft delete records, you use the usual methods: deleteOne or deleteMany on a Query, or you use the unit of work to delete them. The Soft Delete plugin automatically handles in the background the rest.


Deleted records can be restored by using a lifted query via SoftDeleteQuery. It has restoreOne and restoreMany.

import { SoftDeleteQuery } from '@deepkit/orm';

await database.query(User).lift(SoftDeleteQuery).filter({ id: 1 }).restoreOne();
await database.query(User).lift(SoftDeleteQuery).filter({ id: 1 }).restoreMany();

The unit of work supports restoring elements as well.

import { SoftDeleteSession } from '@deepkit/orm';

const session = database.createSession();
const user1 = session.query(User).findOne();

await session.commit();

Hard delete

To hard delete records, use a lifted query via SoftDeleteQuery. This restores basically the old behavior without the plugin for a single query.

import { SoftDeleteQuery } from '@deepkit/orm';

await database.query(User).lift(SoftDeleteQuery).hardDeleteOne();
await database.query(User).lift(SoftDeleteQuery).hardDeleteMany();

//those are equal
await database.query(User).lift(SoftDeleteQuery).withSoftDeleted().deleteOne();
await database.query(User).lift(SoftDeleteQuery).withSoftDeleted().deleteMany();

Query deleted

On a lifted query via SoftDeleteQuery you can also include deleted record.

import { SoftDeleteQuery } from '@deepkit/orm';

// find all, soft deleted and not deleted
await database.query(User).lift(SoftDeleteQuery).withSoftDeleted().find();

// find only soft deleted
await database.query(s).lift(SoftDeleteQuery).isSoftDeleted().count()

Deleted by

deletedBy can be set via query and unit of work.

import { SoftDeleteSession } from '@deepkit/orm';

const session = database.createSession();
const user1 = session.query(User).findOne();


await session.commit();
import { SoftDeleteQuery } from '@deepkit/orm';

Made in Germany