Core: Fix missing delete files from transaction (#9354) (#9356)

Co-authored-by: Fokko Driesprong <fokko@apache.org>
diff --git a/core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java b/core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java
index 29cdf71..3fcd6ec 100644
--- a/core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java
+++ b/core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java
@@ -29,7 +29,6 @@
 import java.io.UncheckedIOException;
 import java.util.Arrays;
 import java.util.List;
-import java.util.ListIterator;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
@@ -884,6 +883,7 @@
     return new CreateSnapshotEvent(tableName, operation(), snapshotId, sequenceNumber, summary);
   }
 
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")
   private void cleanUncommittedAppends(Set<ManifestFile> committed) {
     if (cachedNewDataManifests != null) {
       boolean hasDeletes = false;
@@ -899,15 +899,18 @@
       }
     }
 
-    ListIterator<ManifestFile> deleteManifestsIterator = cachedNewDeleteManifests.listIterator();
-    while (deleteManifestsIterator.hasNext()) {
-      ManifestFile deleteManifest = deleteManifestsIterator.next();
-      if (!committed.contains(deleteManifest)) {
-        deleteFile(deleteManifest.path());
-        deleteManifestsIterator.remove();
+    boolean hasDeleteDeletes = false;
+    for (ManifestFile cachedNewDeleteManifest : cachedNewDeleteManifests) {
+      if (!committed.contains(cachedNewDeleteManifest)) {
+        deleteFile(cachedNewDeleteManifest.path());
+        hasDeleteDeletes = true;
       }
     }
 
+    if (hasDeleteDeletes) {
+      this.cachedNewDeleteManifests.clear();
+    }
+
     // rewritten manifests are always owned by the table
     for (ManifestFile manifest : rewrittenAppendManifests) {
       if (!committed.contains(manifest)) {