Index: /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl
===================================================================
--- /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42697)
+++ /branches/eam_branches/ipp-20230313/ippScripts/scripts/xcstack_skycell.pl	(revision 42698)
@@ -284,4 +284,6 @@
 }
 
+delete_temps() unless ($save_temps);
+
 unless ($no_update) {
 
@@ -373,4 +375,37 @@
 }
 
+
+# Ensure temporary images are deleted
+sub delete_temps
+{
+    unless ($temp_images_exist) {
+        print "No temporary images yet generated\n";
+        return 1;
+    }
+
+    print "Ensuring temporary images are deleted.\n";
+
+    my $temp_delete = defined($recipe) and metadataLookupBool($recipe, 'TEMP.DELETE'); # Delete temp files?
+    if ($temp_delete) {
+        my $temp_dir = metadataLookupStr($ipprc->{_siteConfig}, 'TEMP.DIR') or &my_die("Unable to find temporary directory in site configuration", $xcstack_id, $PS_EXIT_CONFIG_ERROR); # Directory with temporary files
+        my $temp_image = metadataLookupStr($recipe, 'TEMP.IMAGE'); # Suffix for image
+        my $temp_mask = metadataLookupStr($recipe, 'TEMP.MASK'); # Suffix for mask
+        my $temp_weight = metadataLookupStr($recipe, 'TEMP.VARIANCE'); # Suffix for weight
+        my $temp_root = basename($outroot); # Root name for temporary files
+
+        if (not defined $temp_dir or not defined $temp_image or not defined $temp_mask or
+            not defined $temp_weight) {
+            &my_die("Unable to find details for temporary images.", $xcstack_id, $PS_EXIT_CONFIG_ERROR);
+        }
+
+        for (my $i = 0; $i < $num; $i++) {
+            foreach my $ext ( $temp_image, $temp_mask, $temp_weight ) {
+                my $file = $temp_dir . '/' . $temp_root . '.' . $i . '.' . $ext;
+                unlink $file if -e $file;
+            }
+        }
+    }
+}
+
 sub prepare_output
 {
