# Generated by Django 3.2.13 on 2022-07-19 10:33

from django.db import migrations

from ...core.tasks import delete_files_from_storage_task


def drop_media_to_remove(apps, _schema_editor):
    ProductMedia = apps.get_model("product", "ProductMedia")
    # delete all related images
    image_paths_to_delete = []
    for media in ProductMedia.objects.filter(to_remove=True).iterator():
        image_paths_to_delete.append(media.image.name)
        for thumbnail in media.thumbnails.all():
            image_paths_to_delete.append(thumbnail.image.name)

    if image_paths_to_delete:
        delete_files_from_storage_task.delay(image_paths_to_delete)

    ProductMedia.objects.filter(to_remove=True).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("product", "0173_create_default_category_and_product_type"),
        ("thumbnail", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(
            drop_media_to_remove,
            reverse_code=migrations.RunPython.noop,
        ),
    ]

@@ -144,193 +144,421 @@
 def 
-drop_media_to_remove(apps, _schema_editor):%0A    ProductMedia = apps.get_model(%22product%22, %22ProductMedia%22)%0A    # delete all related images%0A    image_paths_to_delete = %5B%5D%0A    for media in 
+iter_media_to_delete(qs):%0A    for media in qs.iterator():%0A        yield media.image.name%0A        for thumbnail in media.thumbnails.all():%0A            yield thumbnail.image.name%0A%0A%0Adef drop_media_to_remove(apps, _schema_editor):%0A    ProductMedia = apps.get_model(%22product%22, %22ProductMedia%22)%0A    image_paths_to_delete = %5B%5D%0A    item_per_task = 100%0A%0A    # delete all related images%0A    for path in iter_media_to_delete(
 Prod
@@ -596,26 +596,16 @@
 ve=True)
-.iterator(
 ):%0A     
@@ -640,74 +640,141 @@
 end(
-media.image.name)%0A        for thumbnail in media.thumbnails.all():
+path)%0A        if len(image_paths_to_delete) == item_per_task:%0A            delete_files_from_storage_task.delay(image_paths_to_delete)
 %0A   
@@ -808,35 +808,14 @@
 ete.
-append(thumbnail.image.name
+clear(
 )%0A%0A 

Fix migration not paginating the number of items to send in task (#10777)