Technical Q&A QA1719

How do I prevent files from being backed up to iCloud and iTunes?

Q:  My app has a number of files that need to be stored on the device permanently for my app to function properly offline. However, those files do not contain user data and don't need to be backed up. How can I prevent them from being backed up?

A: On iOS, apps are responsible for ensuring that only user data and not application data is backed up to iCloud and iTunes. The exact steps necessary vary between iOS version, so this QA will describe the process for each version of iOS. For more information on exactly what data should or should not be backed up, see the App Backup Best Practices section of the iOS App Programming Guide.

iOS 5.1 and later

Starting in iOS 5.1, apps can use either NSURLIsExcludedFromBackupKey or kCFURLIsExcludedFromBackupKey file system properties to exclude files and directories from backups. Apps that need to exclude a large number of files can exclude them by creating their own sub-directory and marking that directory as excluded. Apps should create their own directories for exclusion, rather than excluding the system defined directories. Either of these APIs is preferred over the older, deprecated approach of directly setting an extended attribute. All apps running on iOS 5.1 and later should use these APIs to exclude data from backups.

Listing 1  Excluding a File from Backups on iOS 5.1 and later (Objective C)

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
    NSURL* URL= [NSURL fileURLWithPath: filePathString];
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    return success;

Listing 2  Excluding a File from Backups on iOS 5.1 and later (Swift)

 func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool
        let URL:NSURL = NSURL.fileURLWithPath(filePath)
        assert(NSFileManager.defaultManager().fileExistsAtPath(filePath), "File \(filePath) does not exist")
        var success: Bool
        do {
            try URL.setResourceValue(true, forKey:NSURLIsExcludedFromBackupKey)
            success = true
        } catch let error as NSError {
            success = false
            print("Error excluding \(URL.lastPathComponent) from backup \(error)");
        return success

iOS 5.0.1

If your app must support iOS 5.0.1, you can use the following method to set the "do not back up" extended attribute. Whenever you create a file or folder that should not be backed up, write the data to the file and then call this method, passing in a URL to the file.

Listing 3  Setting the Extended Attribute on iOS 5.0.1

#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
    assert([[NSFileManager defaultManager] fileExistsAtPath: filePathString]);
    const char* filePath = [filePathString fileSystemRepresentation];
    const char* attrName = "";
    u_int8_t attrValue = 1;
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    return result == 0;

iOS 5.0

It is not possible to exclude data from backups on iOS 5.0. If your app must support iOS 5.0, then you will need to store your app data in Caches to avoid that data being backed up. iOS will delete your files from the Caches directory when necessary, so your app will need to degrade gracefully if its data files are deleted.

Document Revision History


Added a how-to code snippet in Swift


Fixed typo


Modified code snippets to use a string argument instead of URLs. Updated link to the App Backup Best Practices section in App Programming guide.


Updated for iOS 5.1


-Fixed critical bug in code snippet.


New document that describes how an app can prevent files from being backed up to iCloud and iTunes.