HOW I FIXED APPLE MAIL SYNCING ISSUES WITH GMAIL (AND, I PRESUME ALL IMAPS)
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. 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 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 either messed up caused a glitch when I initially upgraded to Mavericks, and the glitch was forever saved in the iCloud Internet Accounts menu, 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 the iCloud Internet Accounts.
I know the Internet Accounts in the settings menu is now stored in iCloud because I use a MacBook Air, and two different iMacs. As soon as you sign in to iCloud on any Mac, that Internet Accounts screen 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, 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. Delete all other internet accounts after the iCloud deletes them only from the particular machine you're working on.)
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, 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: Readd the iCloud account (which should then add your other email accounts). I did this reluctantly, but it has worked.
Repeat for all your Mavericks device.
Message was edited by: FriarOP