Remove an image from a field

Is it me making things complicated, or is indeed complicated to remove an image from an image field? :thinking: :sweat_smile:

So my questions/concerns are: we have a certain content type that has the usual plone.namedfile.field.NamedBlobImage field.

We are meant to do a clean up on that field, not delete the field, but rather the images stored in them.

On of my co-workers did an upgrade step to go through all those objects from that content type and do:

for brain in brains:
    obj = brain.getObject()
    obj.image_field = None
transaction.commit()

With that, on the next database packing, the physical images on the filesystem will be removed? What about image scales, they are stored in an annotation, right? Are we meant to clean up that manually? :thinking:

What's the default z3c.form doing when one selects the option to remove the image there? I haven't had a look at that, where this code lives actually? :sweat_smile:

gforcada via Plone Community wrote at 2022-3-29 11:23 +0000:

...
One of my co-workers did an upgrade step to go through all those objects from that content type and do:

for brain in brains:
   obj = brain.getObject()
   obj.image_field = None
transaction.commit()

With that, on the next database packing, the physical images on the filesystem will be removed? What about image scales, they are stored in an annotation, right? Are we meant to clean up that manually? :thinking:

When the annotation is on the object (likely) and not the field.

A likely better approach is to look at the API of the image field
and check with what method you set (or delete) the image.
Ideally, this method handles the related data as well.

Plone Foundation Code of Conduct