public function delete(Media $media, $flush = true) { $em = $this->entityManager; $fs = new Filesystem(); $file_path = $this->uploadDir . $media->getUri(); try { $file = new File($file_path); if ($file->isFile() && $file->isWritable()) { $fs->remove($file_path); } } catch (FileNotFoundException $e) { } $em->remove($media); if ($flush) { $em->flush(); } }