Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to delete a file (not delete entity object and not overwrite with another file)? #323

Closed
archie18 opened this issue Oct 16, 2014 · 7 comments
Labels

Comments

@archie18
Copy link

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

@archie18
Copy link
Author

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.

@K-Phoen
Copy link
Collaborator

K-Phoen commented Oct 17, 2014

I close this issue then :)

@archie18
Copy link
Author

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.

@RowanReid
Copy link

This should be mentioned in the documentation somewhere :-)

@K-Phoen
Copy link
Collaborator

K-Phoen commented Aug 5, 2015

The bundled evolved and the correct way to remove a file "manually" is now: $this->get('vich_uploader.upload_handler')->remove($entity, 'fieldName');

@danaki
Copy link

danaki commented Jul 31, 2019

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.

@vbarrier
Copy link

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.

Does it work like that in 2023? (It seems not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants