# Delete Statements¶

There are three different statements in doctrine to delete. This entry shows your possibilities and help you to decide which statement should use.

To understand the statements we will apply them on an example. Think of books and chapters, where book is the parent and chapter is the child and owning entity.

## Orphan Removal¶

Orphan removal delete a child entity on flush if its relation changed. So you need to apply orphan removal on book. You will use Orphan removal every time you use child elements that can be added and deleted. For example in a CollectionType in Symfony forms. Orphan removal don’t have any effects if you just delete books. So if a book will be deleted, the chapter entities still exists.

Book:
oneToMany:
chapters:
targetEntity: Chapter
mappedBy: book
orphanRemoval: true

Chapter:
manyToOne:
book:
targetEntity: Book


So change mean, if you call a setter and change the current member variable of chapter.

<?php

$chapter->setBook($book);     // Set it first time
$em->flush();$chapter->setBook($book); // nothing changed$chapter->setBook($otherBook) // member variable changed$chapter->setOtherBook($book) // member variable changed$em->flush();                 // Orphan removal will be triggered


So if you work with the book class, you need to add setter to it, because chapter is the child but the owning entity

<?php

class Book {

public function addChapter(Chapter $chapter) {$chapter->setBook($this); // This line need to be added$this->chapters[] = $chapter; return$this;
}

public function removeChapter(Chapter $chapter) {$chapter->setBook(null); // This line need to be added
$this->chapters->removeElement($chapter);
}


Cascade remove will call a doctrine remove on all child elements if it was called on the parent. You apply a cascade remove on the parent entity. Normally you use it, if the child element only exists with a parent. So a chapter never exists alone, it will need a book.

Book:
oneToMany:
chapters:
targetEntity: Chapter
mappedBy: book

Chapter:
manyToOne:
book:
targetEntity: Book

<?php

$book->addChapter($chapter)
$em->remove($book);