8

I'm using a UIDocument with iCloud. I'm not using CoreData. What's the best way to delete a UIDocument?

5 Answers 5

16

Copied from the "Deleting a Document" section of the Document-Based App Programming Guide for iOS.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingForDeleting
        error:nil byAccessor:^(NSURL* writingURL) {
        NSFileManager* fileManager = [[NSFileManager alloc] init];
        [fileManager removeItemAtURL:writingURL error:nil];
    }];
});

N.B.: "When you delete a document from storage, your code should approximate what UIDocument does for reading and writing operations. It should perform the deletion asynchronously on a background queue, and it should use file coordination."

2
  • 2
    If you are using a UIDocument there is no need to implement your own NSFileCoordinator it is already baked into UIDocument. Using your method is expensive results in multiple NSFileCoordinator instances. See: developer.apple.com/library/ios/documentation/FileManagement/…
    – Joseph
    Commented Mar 11, 2014 at 9:42
  • 1
    @Joseph I am wondering, from your answer above, is that why I am either unable to delete an item, which is an object of UIDocument, from iCloud or the app hangs when trying to do so? My related question here
    – yohannes
    Commented May 7, 2017 at 11:11
10

To delete the document from iCloud, first you have to get the filename you want to delete. and then you can delete it using NSFileManager.

NSString *saveFileName = @"Report.pdf";
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:saveFileName];
NSFileManager *filemgr = [NSFileManager defaultManager];
[filemgr removeItemAtURL:ubiquitousPackage error:nil];

This is the way, which i used to delete document, Check it out. It is woking great for me. Thanks

5
  • 4
    According to the docs you have to delete stuff async on a background queue. developer.apple.com/library/ios/documentation/DataManagement/…
    – Jonny
    Commented Jun 25, 2012 at 7:09
  • Where this works it does not take advantage of UIDocument's implementation of NSFileCoordinator Adrian Sarli's answer is better.
    – Joseph
    Commented Mar 11, 2014 at 9:43
  • 1
    For reference, please note that removeItemAtURL: will not delete the document if it has not yet been downloaded from iCloud to the device.
    – Mark
    Commented Feb 12, 2015 at 8:45
  • I have used this solution in the past but iCloud works like crap on iOS and this solution will fail constantly. AlexChaffee is the only that works for me 100% of the time.
    – Duck
    Commented Jan 27, 2016 at 19:10
  • @Mark I too found that removeItemAtURL: didn't work while evicted, but trashItemAtURL:resultingItemURL:error: does.
    – Rik Renich
    Commented Mar 26, 2019 at 22:17
6

SWIFT 3 come from @AlexChaffee 's answer

func deleteZipFile(with filePath: String) {
    DispatchQueue.global(qos: .default).async {
        let fileCoordinator = NSFileCoordinator(filePresenter: nil)
        fileCoordinator.coordinate(writingItemAt: URL(fileURLWithPath: filePath), options: NSFileCoordinator.WritingOptions.forDeleting, error: nil) {
            writingURL in
            do {
                try FileManager.default.removeItem(at: writingURL)
            } catch {
                DLog("error: \(error.localizedDescription)")
            }
        }
    }
}
0

I think I found a solution:

[[NSFileManager defaultManager] setUbiquitous:NO itemAtURL:url destinationURL:nil error:nil]

Source: http://oleb.net/blog/2011/11/ios5-tech-talk-michael-jurewitz-on-icloud-storage/

1
  • 1
    That doesn't work- iCloud now ignores this call with a nil-value destinationURL. Commented Jul 3, 2014 at 12:06
0

See Apple documentation of "Managing the Life-Cyle of a Document" under 'Deleting a Document."

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.