Hello,
I'm trying to figure out how to simply delete a file attached to an entity. I do not want to delete the entire entity object and neither do I want to replace the file with another file. Just delete the file. I searched the documentation and tried Google but didn't find anything useful. I also tried to set the file variable of the entity object to NULL and persist the entity. But this did not remove the physical file.
Any hints are highly appreciated.
Best,
Andreas
I posted too quickly. I solved this with the following code:
$this->get('vich_uploader.storage')->remove($entity, 'file_mapping');
$entity->setFile(null);
$entity->setFileName(null);
Followed by persisting the entity.
I close this issue then :)
I believe this behaviour of not deleting the physical file when an entity object's file variable is set null is rather a bug or a missing feature. I think that with
delete_on_update: true
setting the variable to null should be captured by the listener and the file should be deleted automatically.
This should be mentioned in the documentation somewhere :-)
The bundled evolved and the correct way to remove a file "manually" is now: $this->get('vich_uploader.upload_handler')->remove($entity, 'fieldName');
Injection of vich_uploader.upload_handler breaks the abstraction. Please consider implementing this:
setting the variable to null should be captured by the listener and the file should be deleted automatically.
Most helpful comment
I believe this behaviour of not deleting the physical file when an entity object's file variable is set null is rather a bug or a missing feature. I think that with
setting the variable to null should be captured by the listener and the file should be deleted automatically.