orphanRemoval: true in Doctrine
24 March 2026 (Updated 24 March 2026)
Suppose you have a User entity with an articles relation like so:
#[ORM\OneToMany(
mappedBy: 'user',
targetEntity: User::class,
orphanRemoval: true,
cascade: ['persist']
)]
private Collection $articles;
The orphanRemoval: true option tells Doctrine that whenever you remove an Article record from the User->articles collection, it should also delete the “orphaned” Article record from the database.
In other words, if you do something like:
// We make the $article record an "orphan" here
$user->getArticles()->removeElement($article);
$entityManager->flush();
Then, when Doctrine calls the EntityManager::flush() method, it’ll also delete the orphaned $article record from the DB with a DELETE SQL statement.
Tagged:
Doctrine