public function deleteAction(Request $request, $id)
    {
        $mediaManager = $this->get('sulu_media.media_manager');

        /** @var EntityManagerInterface $entityManager */
        $entityManager = $this->get('doctrine.orm.entity_manager');

        /** @var DynamicRepository $repository */
        $repository = $this->get('sulu_form.repository.dynamic');

        /** @var Dynamic $dynamic */
        $dynamic = $repository->find($id);

        $attachments = array_values($dynamic->getFieldsByType(Dynamic::TYPE_ATTACHMENT));

        foreach ($attachments as $mediaIds) {
            foreach ($mediaIds as $mediaId) {
                if ($mediaId) {
                    try {
                        $mediaManager->delete($mediaId);
                    } catch (MediaNotFoundException $e) {
                        // Do nothing when meida was removed before.
                    }
                }
            }
        }
        $entityManager->remove($dynamic);
        $entityManager->flush();

        return new Response('', 204);
    }