NSCocoaErrorDomain Code=513 after user delete's

I work on an app that saves data to the Documents folder in the users iCloud Drive. This uses the iCloud -> iCloud Documents capability with a standard container.

We've noticed an issue where a user will delete the apps data by doing to Settings > {Name} > iCloud > Storage > App Name > select "delete data from iCloud", and then our app can no longer write to or create the Documents folder.

Once that happens, we get this error:

Error Domain=NSCocoaErrorDomain Code=513 "You don't have permission to save the file "Documents" in the folder "iCloud~your~bundle~identifier"." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/iCloud~your~bundle~identifier/Documents, NSURL=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~your~bundle~identifier/Documents, NSUnderlyingError=0x1102c7ea0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}

This is reproducible using the sample project here https://developer.apple.com/documentation/uikit/synchronizing-documents-in-the-icloud-environment.

Steps to reproduce in that project:

  1. Tap the plus sign in the top right corner to create a new document
  2. Add a document name and tap "Save to Documents"
  3. Go to Settings > {Name} > iCloud > Storage > SimpleiCloudDocument App Name > select "delete data from iCloud"
  4. Reopen the app and repeat steps 1-2
  5. Observe error on MainViewController+Document.swift:59

Deleting and reinstalling the app doesn't seem to help.

I can't reproduce the issue you described by building the sample app with Xcode 26.0 beta 5 (17A5295f) and running it on my iPhone 16 Pro + iOS 18.6.1 – After deleting the app's iCloud data with Settings, and going back to the sample app, I see that the documents I created disappears, and can create a new document without any error.

What system version did you test with? I am wondering if the issue only occurs on an old system version...

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I've reproduced this on these 2 setups using the same iCloud account:

  • Xcode 26.0 beta 4 (17A5285i) & iPhone 12 running iOS 26.0 (23A5326a)
  • Xcode 16.4 (16F6) & iPhone 16 Pro running iOS 18.6.1 (22G90)

Deleting the iCloud data for the app from one device also allows me to reproduce the issue on the other device.

The error printed in the sample project by the console is:

You don’t have permission to save the file “Documents” in the folder “iCloud~com~hfriia~SimpleiCloudDocument1”.
Failed to save a document!

I just tested on this setup using another iCloud account and it functions as expected - I can immediately recreate the documents folder and save a document:

  • Xcode 26.0 beta 4 (17A5285i) & iPhone 7 running iOS 15.8.4 (19H390)
  • Xcode 26.0 beta 4 (17A5285i) & iPhone XR running iOS 18.6.1 (22G90)

Maybe there's something wrong with the first iCloud account?

NSCocoaErrorDomain Code=513 after user delete's
 
 
Q