title: Symfony 4 - Good practice to remove your own user account while connected
body: <p>I would like my users to be able to delete their own user account. I made a <code>SecurityController</code> where there is my 3 functions <code>login</code>, <code>logout</code> and <code>deleteUser</code>. When I delete the current user in database this error appears :</p>
  
  <blockquote>
    <p>You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.</p>
  </blockquote>
  
  <p>When I delete another user, it works correctly because he's not connected.
  Do I have to serialize the User and pass it through a Service, logout the user then remove it in a Service? Or can I clear the PHP session in the controller but I don't know how to do it with <a href="/questions/tagged/symfony4" class="post-tag" title="show questions tagged &#39;symfony4&#39;" rel="tag">symfony4</a> I think it changed since version 4.</p>
  
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  
  use App\Entity\User;
  use App\Form\UserType;
  
  class SecurityController extends Controller
  {
    /**
     * @Route("/createAdmin", name="create_admin")
     */
      public function createAdminUser(Request $request, UserPasswordEncoderInterface $passwordEncoder)
      {
        $usersRepo = $this-&gt;getDoctrine()-&gt;getRepository(User::class);
        $uCount = $usersRepo-&gt;countAllUsers();
  
        if ($uCount == 0)
        {
          $user = new User();
          $form = $this-&gt;createForm(UserType::class, $user, array(
            'is_fresh_install' =&gt; true,
          ));
  
          $form-&gt;handleRequest($request);
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
              // Encode the password
              $password = $passwordEncoder-&gt;encodePassword($user, $user-&gt;getPlainPassword());
              $user-&gt;setPassword($password);
  
              // save the User
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($user);
              $em-&gt;flush();
  
              // Do what you want here before redirecting the user
  
              return $this-&gt;redirectToRoute('login');
          }
  
          return $this-&gt;render('security/register_admin_user.html.twig', array(
            'form' =&gt; $form-&gt;createView(),
          ));
        } else {
          if ($this-&gt;getUser())
          {
            return $this-&gt;redirectToRoute('user_account');
          } else {
            return $this-&gt;redirectToRoute('login');
          }
        }
      }
  
      /**
       * @Route("/login", name="login")
       */
      public function login(Request $request, AuthenticationUtils $authUtils)
      {
        $usersRepo = $this-&gt;getDoctrine()-&gt;getRepository(User::class);
        $uCount = $usersRepo-&gt;countAllUsers();
  
        if ($uCount == 0)
        {
          return $this-&gt;redirectToRoute('create_admin');
        } else {
          $error = $authUtils-&gt;getLastAuthenticationError();
          $lastUsername = $authUtils-&gt;getLastUsername();
  
          return $this-&gt;render('security/login.html.twig', array(
           'last_username' =&gt; $lastUsername,
           'error'         =&gt; $error,
          ));
        }
      }
  
      /**
       * @Route("/logout", name="logout")
       */
      public function logout()
      {
  
      }
  
      /**
       * @Route("/delete_user/{id}", name="delete_user")
       */
      public function deleteUser($id)
      {
        $em = $this-&gt;getDoctrine()-&gt;getManager();
        $usrRepo = $em-&gt;getRepository(User::class);
  
        $user = $usrRepo-&gt;find($id);
        $em-&gt;remove($user);
        $em-&gt;flush();
  
        return $this-&gt;redirectToRoute('user_registration');
  
      }
  
  }
  </code></pre>