Index: trunk/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24412)
+++ trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24500)
@@ -1581,26 +1581,32 @@
 TRANS: while (1) {
         eval {
+            my $instances;
             my $so_id;
-            my $instances;
-            # get so_id
+            my $ins_id;
+            # find so_id for key and get count of instances
             {
-                my $query = $db->prepare_cached( $sql->get_object_from_uri );
-                my $rows = $query->execute( $uri );
-
+                my $query = $db->prepare_cached( $sql->get_instance_count_by_ext_id );
+                my $rows = $query->execute($key->path);
                 unless ( $rows > 0 ) {
                     $query->finish;
+                    die( "$key has no associated instances - this should not happen" );
+                }
+
+                my $record = $query->fetchrow_hashref;
+                $so_id      = $record->{ 'so_id' };
+                $instances  = $record->{ 'count(ins_id)' };
+                $query->finish;
+            }
+
+            # find ins_id for uri
+            {
+                my $query = $db->prepare_cached( $sql->get_instance_by_uri );
+                my $rows = $query->execute($so_id, $uri);
+                unless ( $rows > 0) {
+                    $query->finish;
                     die( "no instance is associated with uri" );
                 }
 
-                $so_id = $query->fetchrow_hashref->{ 'so_id' };
-                $query->finish;
-
-            }
-
-            {
-                my $query = $db->prepare_cached( $sql->get_instance_count );
-                $query->execute( $so_id );
-
-                $instances = $query->fetchrow_hashref->{ 'count(ins_id)' };
+                $ins_id = $query->fetchrow_hashref->{ 'ins_id' };
                 $query->finish;
             }
@@ -1608,10 +1614,10 @@
             # remove instance
             {
-                my $query = $db->prepare_cached( $sql->delete_instance );
-                my $rows = $query->execute( $uri );
+                my $query = $db->prepare_cached( $sql->delete_instance_by_ins_id );
+                my $rows = $query->execute( $ins_id );
                 $query->finish;
                 
-                # if we affected something other then two rows something very bad
-                # has happened
+                # if we affected something other then one row something very
+                # bad has happened
                 unless ( $rows == 1 ) {
                     die( "affected row count is $rows instead of 1" );
