--- /dev/null
+Return-Path: <dkg@fifthhorseman.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 39C3F6DE0360\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Apr 2016 18:03:02 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id tyJciQat_1Qy for <notmuch@notmuchmail.org>;\r
+ Fri, 8 Apr 2016 18:02:54 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id C9EFA6DE02D0\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Apr 2016 18:02:40 -0700 (PDT)\r
+Received: from fifthhorseman.net (unknown [201.140.212.132])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 5538C10080\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Apr 2016 21:02:39 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id 0E84B200E0; Fri, 8 Apr 2016 22:02:35 -0300 (ART)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v3 7/7] complete ghost-on-removal-when-shared-thread-exists\r
+Date: Fri, 8 Apr 2016 22:02:34 -0300\r
+Message-Id: <1460163754-22994-7-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\r
+In-Reply-To: <1460163754-22994-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
+ <1460163754-22994-1-git-send-email-dkg@fifthhorseman.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 09 Apr 2016 01:03:02 -0000\r
+\r
+To fully complete the ghost-on-removal-when-shared-thread-exists\r
+proposal, we need to clear all ghost messages when the last active\r
+message is removed from a thread.\r
+---\r
+ lib/message.cc | 20 ++++++++++++++++++++\r
+ test/T590-thread-breakage.sh | 6 +-----\r
+ 2 files changed, 21 insertions(+), 5 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 1b423b0..39dbe53 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1098,6 +1098,26 @@ _notmuch_message_delete (notmuch_message_t *message)\r
+ \r
+ notmuch_message_destroy (ghost);\r
+ status = COERCE_STATUS (private_status, "Error converting to ghost message");\r
++ } else {\r
++ /* the thread is empty; drop all ghost messages from it */\r
++ notmuch_messages_t *messages;\r
++ status = _notmuch_query_search_documents (query,\r
++ "ghost",\r
++ &messages);\r
++ if (status == NOTMUCH_STATUS_SUCCESS) {\r
++ notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;\r
++ while (notmuch_messages_valid (messages)) {\r
++ message = notmuch_messages_get (messages);\r
++ status = _notmuch_message_delete (message);\r
++ if (status) /* we'll report the last failure we see;\r
++ * if there is more than one failure, we\r
++ * forget about previous ones */\r
++ last_error = status;\r
++ notmuch_message_destroy (message);\r
++ notmuch_messages_move_to_next (messages);\r
++ }\r
++ status = last_error;\r
++ }\r
+ }\r
+ notmuch_query_destroy (query);\r
+ return status;\r
+diff --git a/test/T590-thread-breakage.sh b/test/T590-thread-breakage.sh\r
+index 22e5cbc..969243e 100755\r
+--- a/test/T590-thread-breakage.sh\r
++++ b/test/T590-thread-breakage.sh\r
+@@ -121,10 +121,6 @@ notmuch new >/dev/null\r
+ test_thread_count 0 'All messages gone: no threads'\r
+ test_subject_count apple 0\r
+ test_subject_count banana 0\r
+-test_begin_subtest 'No ghosts should remain after full thread deletion'\r
+-# this is known to fail; we are leaking ghost messages deliberately\r
+-test_subtest_known_broken\r
+-ghosts=$(../ghost-report ${MAIL_DIR}/.notmuch/xapian)\r
+-test_expect_equal "$ghosts" "0"\r
++test_ghost_count 0 'No ghosts should remain after full thread deletion'\r
+ \r
+ test_done\r
+-- \r
+2.8.0.rc3\r
+\r