I’ve been looking everywhere but couldn’t find the solution. When I update the picture of a user, I want the old image to be deleted. Uploading and overwriting the user picture works. I’m also able to get the filepath. But sadly not deleting.
public function submitForm(array &$form, FormStateInterface $form_state){ $userId = Drupal::currentUser()->id(); $currentUser = User::load($userId); $image = $currentUser->user_picture->entity->getFileUri(); $values = $form_state->getValue('profile_image'); $currentUser->set('user_picture', $form_state->getValue('profile_image')); $currentUser->save(); $this->messenger()->addStatus($this->t('Thanks for submmission!!')); }