Repository: thundernest/k-9
Issue: some folders are not removed from K9 after a refresh folder list action

hi all

firstly, i have searched for "folders not removed". I've looked at both open and closed issues and can't seem to find anything that matches my problem. This of course makes me wonder if it's something specific to me.



### **Describe the bug**
when i've renamed a folder in gmail, i would expect the k9 refresh folder list action to:

a) produce the newly named folder

b) remove the old / missing folder

As per your FAQ here https://k9mail.app/documentation/faq.html#anchor16

However, I've got K9 folders that no longer exist in the browser gmail experience (because i've renamed them), yet they are still visible in K9.

Refresh Folder List does not remove them.



### **To Reproduce**
i can only partially reproduce it now :(

I've literally just tested with a new folder in browser gmail (no emails in test folder), just so as to not waste your time.

- I refreshed the k9 folder list. New folder appeared.
- i then renamed in gmail (browser) and k9 folder refreshing changed the folder name. The previous folder name no longer exists in K9.
- As a final test, i deleted the gmail folder ("remove label" command) and K9 folder refreshed. Folder disappears.

I retested the above with an email in it (admittedly a draft email).

- Test folder with emails in it appears in K9.
- Renamed test folder with email in it appears in K9.
- Old test folder with email disappears from K9.
- Removing test folder from gmail removes from K9 after folder refresh.

**However**, i DO have existing K9 folders which don't exist on gmail any more, yet i can't delete them from K9 because there's no delete function and refresh folder list doesn't remove them. 

- Although there were no emails showing the redundant K9 folders, i tried "clear local messages", in case there was some data marked against them. This didn't help.
- I've got 8 folders in K9 like this, which don't exist on gmail.
- I did think about the possibility of compacting the account but i think that functionality has been removed.



### **Expected behaviour**
all folders that no longer exist in gmail should get removed by K9's refresh folder list action.



### **Additional context**
using K9 v5.806 on Samsung S8 with official OTA android release 9 i.e. pie

Am using IMAP for gmail to K9 operations.



### **Logs**
~~didn't attach any as i get no errors~~

debug log attached for "Refresh Folder List" action on my the account in question.
[k9-log.txt](https://github.com/k9mail/k-9/files/7155866/k9-log.txt)




regards,

Gary

Changed files:
app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt

diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt b/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt
index 14dc62418..f40136d34 100644
--- a/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt
+++ b/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt
@@ -57,21 +57,28 @@ class FolderRepository(
 
     fun getDisplayFoldersFlow(account: Account, displayMode: FolderMode): Flow<List<DisplayFolder>> {
         val messagingController = DI.get<MessagingController>()
+        val messageStore = messageStoreManager.getMessageStore(account)
 
         return callbackFlow {
             send(getDisplayFolders(account, displayMode))
 
-            val listener = object : SimpleMessagingListener() {
+            val folderStatusChangedListener = object : SimpleMessagingListener() {
                 override fun folderStatusChanged(statusChangedAccount: Account, folderId: Long) {
                     if (statusChangedAccount.uuid == account.uuid) {
                         sendBlockingSilently(getDisplayFolders(account, displayMode))
                     }
                 }
             }
-            messagingController.addListener(listener)
+            messagingController.addListener(folderStatusChangedListener)
+
+            val folderSettingsChangedListener = FolderSettingsChangedListener {
+                sendBlockingSilently(getDisplayFolders(account, displayMode))
+            }
+            messageStore.addFolderSettingsChangedListener(folderSettingsChangedListener)
 
             awaitClose {
-                messagingController.removeListener(listener)
+                messagingController.removeListener(folderStatusChangedListener)
+                messageStore.removeFolderSettingsChangedListener(folderSettingsChangedListener)
             }
         }.buffer(capacity = Channel.CONFLATED)
             .distinctUntilChanged()