Index: trunk/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24607)
+++ trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24637)
@@ -56,5 +56,5 @@
     my ($config) = @_;
 
-    # log4perl is not avaliable until we call init()
+    # log4perl is not available until we call init()
     Nebulous::Server::Log->init($config);
     my $log = Log::Log4perl::get_logger( "Nebulous::Server" );
@@ -1622,5 +1622,6 @@
 TRANS: while (1) {
         eval {
-            my $instances;
+            my $total;
+            my $available;
             my $so_id;
             my $ins_id;
@@ -1636,5 +1637,6 @@
                 my $record = $query->fetchrow_hashref;
                 $so_id      = $record->{ 'so_id' };
-                $instances  = $record->{ 'count(ins_id)' };
+                $total      = $record->{ 'total' };
+                $available  = $record->{ 'available' };
                 $query->finish;
             }
@@ -1664,13 +1666,29 @@
                     die( "affected row count is $rows instead of 1" );
                 }
-            }
-
-            # if we just deleted the last instance associated with a storage object
-            # remove it too
-            if ( $instances == 1 ) {
+                
+            }
+
+            # if we just deleted the last 'available' instance associated with
+            # a storage object remove it too
+            if ( $available == 1 ) {
                 # remove key from cache
                 $self->cache->delete($key->path) if defined $self->cache;
 
-                # we just removed the last instance
+                # record the path of the innaccesible files for deferred
+                # deletion
+                {
+                    my $query = $db->prepare_cached( $sql->copy_instances_to_deleted );
+                    $query->execute( $so_id );
+                }
+                # remove all instances... not strictly nessicary as the delete
+                # from storage_object should cascade but the fkey was specified
+                # without the cascade 
+                {
+                    my $query = $db->prepare_cached( $sql->delete_instance_by_so_id );
+                    $query->execute( $so_id );
+                }
+                
+                # delete the storage object, remaining instances should be
+                # removed via cascading delete
                 my $query = $db->prepare_cached( $sql->delete_object );
                 my $rows = $query->execute( $so_id );
