What’s the difference between remove and delete?
From Repo:
-
remove
- Removes a given entity or array of entities. It removes all given entities in a single transaction (in the case of entity, manager is not transactional).
Example:
await repository.remove(user);
await repository.remove([
category1,
category2,
category3
]);
-
delete
- Deletes entities by entity id, ids or given conditions:
Example:
await repository.delete(1);
await repository.delete([1, 2, 3]);
await repository.delete({ firstName: "Timber" });
As stated in example here:
import {getConnection} from "typeorm";
await getConnection()
.createQueryBuilder()
.delete()
.from(User)
.where("id = :id", { id: 1 })
.execute();
Which means you should use
remove
if it contains an array of Entities.While you should use
delete
if you know the condition.
Additionally, as @James stated in comment Entity Listener
such as @BeforeRemove
and @AfterRemove
listeners only triggered when the entity is removed using repository.remove
.
Similarly, @BeforeInsert
, @AfterInsert
, @BeforeUpdate
, @AfterUpdate
only triggered when the entity is inserted/updated using repository.save
.
Source: Entity Listeners and Subscribers