Search code examples
mysqldatabasesymfonydoctrinedql

Doctrine - delete all entities


I have problem with deleting all rows in database. I can't find out how to do it. I'm using Symfony and Doctrine. Somewhere I read, that it isn't possible "normal" way, but I can do it by DQL (createQuery), but I don't know syntax.

public function resetDatabase(EntityManagerInterface $em)
{
    $query = $em->createQuery('DELETE ???');
    $query->execute();

    return new Response('', Response::HTTP_OK);
}

Solution

  • public function resetDatabase(EntityManagerInterface $em)
    {
        $query = $em->createQuery(
              'DELETE FROM App\Entity\YourEntity e WHERE e.age > :ageparameter'
           )->setParameter('ageparameter', 10)->execute();
    
        return new Response('', Response::HTTP_OK);
    }