--- /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 8F8376DE02D9\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:16:11 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.274\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.274 tagged_above=-999 required=5\r
+ tests=[AWL=-0.274] 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 UDpM3KWR5O7w for <notmuch@notmuchmail.org>;\r
+ Sat, 2 Apr 2016 07:16:04 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 9C1446DE02DA\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:15:50 -0700 (PDT)\r
+Received: from fifthhorseman.net (dhcp-a244.meeting.ietf.org [31.133.162.68])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 16DD0F997\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 10:15:49 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id BA72920CDE; Sat, 2 Apr 2016 11:15:41 -0300 (BRT)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v2 7/7] complete ghost-on-removal-when-shared-thread-exists\r
+Date: Sat, 2 Apr 2016 11:15:41 -0300\r
+Message-Id: <1459606541-23889-7-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\r
+In-Reply-To: <1459606541-23889-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
+ <1459606541-23889-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, 02 Apr 2016 14:16:11 -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
+ 1 file changed, 20 insertions(+)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index f715e39..01daa56 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1097,6 +1097,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_messages_type_st (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
+-- \r
+2.8.0.rc3\r
+\r