Index: trunk/ippScripts/scripts/stack_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/stack_skycell.pl	(revision 20929)
+++ trunk/ippScripts/scripts/stack_skycell.pl	(revision 21013)
@@ -75,4 +75,5 @@
 my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
 my $ppStack = can_run('ppStack') or (warn "Can't find ppStack" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warp "Can't find ppConfigDump" and $missing_tools = 1);
 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 if ($missing_tools) {
@@ -291,4 +292,10 @@
 
     carp($msg);
+
+    unless ($save_temps) {
+	print "Ensuring temporary images are deleted.";
+	delete_temps();
+    }
+
     if (defined $stack_id and not $no_update) {
         my $command = "$stacktool -stack_id $stack_id -code $exit_code";
@@ -307,4 +314,40 @@
 }
 
+# Ensure temporary images are deleted
+sub delete_temps
+{
+    my $command = "$ppConfigDump -camera $camera -recipe PPSTACK $recipe_ppStack -dump-recipe PPSTACK -";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppConfigDump: $error_code", $stack_id, $error_code);
+    }
+    my $md = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $stack_id, $PS_EXIT_PROG_ERROR);
+
+    my $temp_delete = metadataLookupBool($md, 'TEMP.DELETE'); # Delete temporary files?
+    if ($temp_delete) {
+	my $temp_dir = metadataLookupStr($md, 'TEMP.DIR'); # Directory with temporary files
+	my $temp_image = metadataLookupStr($md, 'TEMP.IMAGE'); # Suffix for image
+	my $temp_mask = metadataLookupStr($md, 'TEMP.IMAGE'); # Suffix for mask
+	my $temp_weight = metadataLookupStr($md, 'TEMP.WEIGHT'); # 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.", $stack_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;
+	    }
+	}
+    }
+}
+
+
 END {
     my $exit = $?;
