TypeOrm
  • Getting Started
  • About
  • Connection
    • Working with Connection
    • Using ormconfig.json
    • Connection Options
    • Multiple connections
    • Connection APIs
  • Entity
    • What is Entity?
    • Embedded Entities
    • Entity Inheritance
    • Tree Entities
    • View Entities
    • Separating Entity Definition
  • Relations
    • What are Relations?
    • One-to-One
    • Many-to-one and One-to-Many
    • Many-to-Many
    • Eager and Lazy Relations
    • Relations FAQ
  • Entity Manager and Repository
    • What is EntityManager
    • Working with Repository
    • Find Options
    • Custom Repositories
    • Entity Manager API
    • Repository API
  • Query Builder
    • Select using Query Builder
    • Insert using Query Builder
    • Update using Query Builder
    • Delete using Query Builder
    • Working with Relations
    • Caching Results
  • Advanced Topics
    • Using CLI
    • Logging
    • Listeners and Subscribers
    • Indices
    • Transactions
    • Migrations
  • Guides
    • Active Record vs Data Mapper
    • Working with MongoDB
    • Using Validation
    • Example with Express
    • Usage with JavaScript
    • Migration from Sequelize
  • Help
    • FAQ
    • Supported Platforms
    • Decorators reference
    • Roadmap
    • Changelog
Powered by GitBook
On this page
  • Delete
  • Soft-Delete
  • Restore-Soft-Delete

Was this helpful?

  1. Query Builder

Delete using Query Builder

Delete

You can create DELETE queries using QueryBuilder. Examples:

import {getConnection} from "typeorm";

await getConnection()
    .createQueryBuilder()
    .delete()
    .from(User)
    .where("id = :id", { id: 1 })
    .execute();

This is the most efficient way in terms of performance to delete entities from your database.

Soft-Delete

Applying Soft Delete to QueryBuilder

import {createConnection} from "typeorm";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    await connection
      .getRepository(Entity)
      .createQueryBuilder()
      .softDelete()

}).catch(error => console.log(error));

Restore-Soft-Delete

Alternatively, You can recover the soft deleted rows by using the restore() method:

import {createConnection} from "typeorm";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    await connection
      .getRepository(Entity)
      .createQueryBuilder()
      .restore()

}).catch(error => console.log(error));
PreviousUpdate using Query BuilderNextWorking with Relations

Last updated 4 years ago

Was this helpful?