Fixing iWork Corrupting Documents in iCloud

Back in July I wrote about recovering corrupted iWork documents from iCloud. Well, since then I've learnt two things:

  1. The contents of a corrupted document package can be copied from the Finder by navigating to the ~/Library/Mobile Documents/com~apple~Pages/Documents/ folder, right-clicking the corrupted document and selecting 'Show Package Contents' (i.e. this doesn't need to be done from the terminal, although deleting the corrupted document does); and
  2. How to address ongoing corruption issues.

Number one is self-explanatory, but in the case of number two, since my last post, every new document I created would become corrupted—although not immediately, which made the problem very hard to diagnose. Even more bizarrely, pre-existing documents that weren't corrupted continued to work without issue. However, after doing some searching online it appeared the cause was likely that the iCloud data store on one of my computers had been corrupted and was corrupting new documents when it synced. And the solution was to disable 'Documents & Data' syncing in the iCloud preferences, which deletes the local iCloud data store, and then re-enable it to recreate the data store (which also re-downloads the documents from iCloud, so nothing is actually lost through this process).

With my computers spread across different locations, and the apparent inconsistency of when documents would become corrupted, I wasn't sure which computer was the culprit, so ended up undertaking this process on each of them. However, at the end I was still encountering the corruption issue. So as a last ditch effort I disabled and re-enabled the setting on my iPad, which appears was the device causing the problem.

Since taking this action my iCloud syncing of iWork documents has been working without issue. So if you're having this same issue, this may be the solution for you as well.

Recovering Corrupted iWork Documents from iCloud

With Mountain Lion on my Macs—now including the machine I use at work—I’ve started to make more use of iCloud document syncing so that I can access my documents at home, work, on my laptop and on my iPad (and occasionally iPhone). But I’ve now been stung twice by corrupted iWork documents.

The first time was a couple of months back with a Numbers document I use to track some personal finances. I don’t know what the corruption was, or what caused it, but it prevented me opening the corrupted file, or copying or moving it, to try and open it elsewhere. Luckily however, I was able to restore to a recent backup from Time Machine[1].

But today I was struck with the issue again with a Keynote presentation that I had created at work. On this occasion there was no Time Machine backup, as I don’t have Time Machine set up on my work computer[2], and I hadn’t had my home Mac on since creating the file for a Time Machine backup to be created there.

So, after some consideration I experimented with a solution that worked for me on this occasion (your mileage may vary).

As I couldn’t copy, move or export the file from Keynote’s iCloud window, the iWork for iCloud beta, or from the ~/Library/Mobile Documents/com~apple~Keynote/Documents/ folder (where iCloud stores Keynote documents locally), I thought (correctly) that I may be able to copy it from the Terminal.

After firing up the Terminal app and navigating to the ~/Library/Mobile Documents/com~apple~Keynote/Documents/ folder I used the cp command to copy the file contents to a new folder on my desktop (using the -r flag on the cp command, as the Keynote file is really a disguised folder/directory)[3].

With the Keynote file contents now in a folder on my desktop, I used the Finder to rename the folder with a .key extension, turning the folder into a Keynote file, which opened without issue. I was then able to copy that working Keynote file back into iCloud, where it seems to have been working fine since.

I’m disappointed, and a little unsettled, by these corruption issues, but relieved I’ve been able to recover files on both instances. When it works, iCloud is fantastic, and the best way to work across Macs and iOS devices on documents. But I’ll be carrful about using it for anything too critical given the issues I’ve experienced, and maybe stick with DropBox for them instead.

Update: A newer post on fixing this problem can be found here.

  1. If I recall correctly, I had to do this in the Finder, after accessing the ~/Library/Mobile Documents/ folder (where iCloud stores its documents locally), and even then I don’t think I was able to move/copy the file back into iCloud, but had to create a new file, copying the contents from the recovered file saved locally.  ↩

  2. The work machine does used server-based network accounts, that in theory would be backed up, but I was after a quicker solution than contacting IT in the hope they could recover the file from a tape somewhere.  ↩

  3. i.e. cp -r <keynotefilename> ~/Desktop/<foldername>/  ↩