REVISED HOW I FIXED APPLE MAIL SYNCING ISSUES WITH GMAIL (AND, I PRESUME ALL IMAPS)
**Revised to add a Step 8 on repeating the process for your other Macs after you do the first one**
**Revised for additional clarity (hopefully)**
----
In our long discussion on this, and my own tinkering, I suspected this issue had something to do with Mavericks now keeping all our internet accounts information in the cloud (through the Keychain?). If you go to settings, Internet Accounts, you can see the list of your various internet accounts. It is no longer possible to completely remove an email account in the Mail app. You can only disable it or enable it. To remove the account, you have to delete it from the Internet Accounts menu in settings.
Many users in these forums have suggested deleting the Mail folder in ~/Library then restarting Mail to have it rebuild all your internet accounts. In this scenario, when you restart Apple Mail you're asked to type in the information for an email account. However, I noticed each time I did this within seconds the new Mail folder created in ~/Library upon restart of the Mail app would be populated with information from all of my email accounts and not just the single one I had entered when the Apple Mail was started.
I have suspected that this was part of the problem. Perhaps my settings and such were messed up and that caused a glitch when I initially upgraded to Mavericks, and the glitch was forever saved in the Internet Accounts menu (through Keychain), or in all of my messing around before the so-called fix was issued I had messed something up, which, again, was set in stone in Internet Accounts because of iCloud not saves the information for all your internet accounts.
I know the Internet Accounts in the settings menu is now stored in iCloud (Keychain) because I use a MacBook Air, and two different iMacs. As soon as you sign in to iCloud on any Mac, and turn on Keychain, that Internet Accounts screen your system settings is populated with all your internet accounts.
So here's what I did to get a complete fresh start and it has worked for me.
Step 1: Quit Apple Mail
Step 2: Go to Settings --> Internet Accounts. Delete the Gmail (or the offending IMAP account) account (you'll be asked to remove it across all your OS X computers--say yes). I mean DELETE, not just disable. Hit the minus sign at the bottom left of the list with Gmail highlighted.
Step 3: Then delete the iCloud account as you did the Gmail account.
Step 4: Then delete the rest of the internet accounts on the computer.
(I think this sequence is important. You are deleting the offending email account from the iCloud Keychain, which should remove it from all your computers. Then you're deleting the iCloud account, which leaves the other internet accounts you have intact on your computer and in the information intact in iCloud Keychain. But you also need to delete the other internet accounts from your settings to get the true clean start for Apple Mail.)
Step 5: Go to the Library folder in your users file. (Which you can do by going to the Finder, selecting Go from the top menu, then selecting Go to Folder... typing in this: ~/Library.) Find the Mail folder and drag it to the trash. Empty the trash. If you get a message that a file is in use and so the process can't be completed, restart the computer then empty trash immediately.
Step 6: Open Mail App. It will ask you to put in information for an email account. Put your GMAIL account in (or your IMAP) account FIRST!! (A MUST!) Give it some time to get everything in there. At this point, you will notice that the problem is gone. Your mail will sync properly, archived/deleted messages will move properly, and read/unread counters will function properly.
Step 7: Add the iCloud account from the Internet Accounts screen. Once you turn on Keychain and get it approved through another device (which should then add your other email accounts). I did this reluctantly, but it has worked. My other email accounts were added to Mail (the ones I didn't delete BEFORE I deleted the iCloud account back in Step 2).
Step 8: When repeating this on your next Mac running for Mavericks, delete iCloud FIRST from Internet Accounts in System Settings, then proceed with Step 4.