Delete using Query Builder

Delete

You can create DELETE queries using QueryBuilder. Examples:
1
import {getConnection} from "typeorm";
2
​
3
await getConnection()
4
.createQueryBuilder()
5
.delete()
6
.from(User)
7
.where("id = :id", { id: 1 })
8
.execute();
Copied!
This is the most efficient way in terms of performance to delete entities from your database.
​

Soft-Delete

Applying Soft Delete to QueryBuilder
1
import {createConnection} from "typeorm";
2
import {Entity} from "./entity";
3
​
4
createConnection(/*...*/).then(async connection => {
5
​
6
await connection
7
.getRepository(Entity)
8
.createQueryBuilder()
9
.softDelete()
10
​
11
}).catch(error => console.log(error));
Copied!

Restore-Soft-Delete

Alternatively, You can recover the soft deleted rows by using the restore() method:
1
import {createConnection} from "typeorm";
2
import {Entity} from "./entity";
3
​
4
createConnection(/*...*/).then(async connection => {
5
​
6
await connection
7
.getRepository(Entity)
8
.createQueryBuilder()
9
.restore()
10
​
11
}).catch(error => console.log(error));
Copied!