diff --git a/changelogs/unreleased/fix-more-orphans-remove-undeleted-groups.yml b/changelogs/unreleased/fix-more-orphans-remove-undeleted-groups.yml
new file mode 100644
index 0000000000000000000000000000000000000000..bc2068b8177dda8f9c456ca14d88fae529e3c20c
--- /dev/null
+++ b/changelogs/unreleased/fix-more-orphans-remove-undeleted-groups.yml
@@ -0,0 +1,4 @@
+---
+title: Remove extra orphaned rows when removing stray namespaces
+merge_request: 7841
+author: 
diff --git a/db/migrate/20161117114805_remove_undeleted_groups.rb b/db/migrate/20161117114805_remove_undeleted_groups.rb
index 696914f8e4dca6a0fb0837d1c19339530c3bd65d..29040583aa25367eb1788ee270777570ad62d252 100644
--- a/db/migrate/20161117114805_remove_undeleted_groups.rb
+++ b/db/migrate/20161117114805_remove_undeleted_groups.rb
@@ -5,47 +5,87 @@ class RemoveUndeletedGroups < ActiveRecord::Migration
   DOWNTIME = false
 
   def up
+    is_ee = defined?(Gitlab::License)
+
+    if is_ee
+      execute <<-EOF.strip_heredoc
+      DELETE FROM path_locks
+      WHERE project_id IN (
+        SELECT project_id
+        FROM projects
+        WHERE namespace_id IN (#{namespaces_pending_removal})
+      );
+      EOF
+
+      execute <<-EOF.strip_heredoc
+      DELETE FROM remote_mirrors
+      WHERE project_id IN (
+        SELECT project_id
+        FROM projects
+        WHERE namespace_id IN (#{namespaces_pending_removal})
+      );
+      EOF
+    end
+
     execute <<-EOF.strip_heredoc
-    DELETE FROM projects
-    WHERE namespace_id IN (
-      SELECT id FROM (
-        SELECT id
-        FROM namespaces
-        WHERE deleted_at IS NOT NULL
-      ) namespace_ids
+    DELETE FROM lists
+    WHERE label_id IN (
+      SELECT id
+      FROM labels
+      WHERE group_id IN (#{namespaces_pending_removal})
+    );
+    EOF
+
+    execute <<-EOF.strip_heredoc
+    DELETE FROM lists
+    WHERE board_id IN (
+      SELECT id
+      FROM boards
+      WHERE project_id IN (
+        SELECT project_id
+        FROM projects
+        WHERE namespace_id IN (#{namespaces_pending_removal})
+      )
     );
     EOF
 
-    if defined?(Gitlab::License)
+    execute <<-EOF.strip_heredoc
+    DELETE FROM labels
+    WHERE group_id IN (#{namespaces_pending_removal});
+    EOF
+
+    execute <<-EOF.strip_heredoc
+    DELETE FROM boards
+    WHERE project_id IN (
+      SELECT project_id
+      FROM projects
+      WHERE namespace_id IN (#{namespaces_pending_removal})
+    )
+    EOF
+
+    execute <<-EOF.strip_heredoc
+    DELETE FROM projects
+    WHERE namespace_id IN (#{namespaces_pending_removal});
+    EOF
+
+    if is_ee
       # EE adds these columns but we have to make sure this data is cleaned up
       # here before we run the DELETE below. An alternative would be patching
       # this migration in EE but this will only result in a mess and confusing
       # migrations.
       execute <<-EOF.strip_heredoc
       DELETE FROM protected_branch_push_access_levels
-      WHERE group_id IN (
-        SELECT id FROM (
-          SELECT id
-          FROM namespaces
-          WHERE deleted_at IS NOT NULL
-        ) namespace_ids
-      );
+      WHERE group_id IN (#{namespaces_pending_removal});
       EOF
 
       execute <<-EOF.strip_heredoc
       DELETE FROM protected_branch_merge_access_levels
-      WHERE group_id IN (
-        SELECT id FROM (
-          SELECT id
-          FROM namespaces
-          WHERE deleted_at IS NOT NULL
-        ) namespace_ids
-      );
+      WHERE group_id IN (#{namespaces_pending_removal});
       EOF
     end
 
-    # This removes namespaces that were supposed to be soft deleted but still
-    # reside in the database.
+    # This removes namespaces that were supposed to be deleted but still reside
+    # in the database.
     execute "DELETE FROM namespaces WHERE deleted_at IS NOT NULL;"
   end
 
@@ -54,4 +94,12 @@ class RemoveUndeletedGroups < ActiveRecord::Migration
     # If someone is trying to rollback for other reasons, we should not throw an Exception.
     # raise ActiveRecord::IrreversibleMigration
   end
+
+  def namespaces_pending_removal
+    "SELECT id FROM (
+      SELECT id
+      FROM namespaces
+      WHERE deleted_at IS NOT NULL
+    ) namespace_ids"
+  end
 end