88

For example, I have a TypeORM entity Profile:

@Entity()
class Profile {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    gender: string;

    @Column()
    photo: string;

    @OneToOne(type => User, { cascade: true })
    @JoinColumn()
    user: User;
}

And I’m not sure which one should I use to delete a user profile?

Profile.remove(profile)
Profile.delete(profile)

What is the difference between the remove and delete methods in TypeORM?

1
  • I guess they have the meanings described here? github.com/typeorm/typeorm/blob/master/docs/… remove() being the one you should use when you have a profile object and delete() deleting based on criteria.
    – Ry-
    Commented Jan 18, 2019 at 1:39

1 Answer 1

150

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

8
  • do lifecycle methods happen the same on both?
    – Jonathan
    Commented Jul 25, 2019 at 12:51
  • 29
    Also be aware that repository.delete does not trigger @Before/After Remove listeners, only repository.remove does. Same way, repository.update does NOT trigger @Before/After Update listeners, only repository.save does.
    – James
    Commented Jul 29, 2019 at 8:32
  • The information that Before/After-Insert only trigger with repository.save is incorrect! I have tried and it did trigger with repository.insert too. From this discussion, pleerock indirectly confirm that insert will also trigger the decorator.
    – nyoto arif
    Commented Jan 6, 2021 at 3:26
  • @nyotoarif Is .insert using Subscribers now? If it does you could request documentation changes for which listener was affected. I've checked Docs and it still states that only .save triggered them. I haven't got the time to test them, I will update once I confirm if it does.
    – Mukyuu
    Commented Jan 6, 2021 at 4:12
  • 1
    Did not find in the documentation, but remove() also seems to mutate passed object by deleting the id field...
    – Klesun
    Commented Mar 11, 2021 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.