How can I delete a plist programmatically?
Asked Answered
H

4

6

I want to delete my entire plist file from the filesystem programmatically. How can I do this?

This is how I am writing the plist:

+ (void)writeObjectToPList:(id)myData {  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [myData writeToFile:path atomically:YES];
}  
Hoisch answered 15/8, 2011 at 17:37 Comment(0)
C
16

NSFileManager will allow you to delete your file using removeItemAtPath:error:

+ (void)deletePList {  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];

    NSError *error;
    if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
    {
        //TODO: Handle/Log error
    }
}
Coltin answered 15/8, 2011 at 17:40 Comment(0)
E
3

Here's how.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [fileManager removeItemAtPath: fullPath error:NULL];
Eduction answered 15/8, 2011 at 17:40 Comment(1)
You wouldn't want to have error null, you'd want to do something like what Joe is doing where he's handling the error if it does exist.Affectation
F
0

This is how I've managed to delete all old plists except the current one I had. It's in Swift 3:

func removeOldPlistFiles() {

    let fileManager = FileManager.default

    // get directory with your current plist for comparison later
    let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let destPath = (dir as NSString).appendingPathComponent("YourCurrentPlistName.plist")

    // Get the documents directory url
    let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

    do {

        // Get the documents directory contents urls (including subfolders urls)
        let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])

        // filter the directory contents and get file names
        let plistFiles = directoryContents.filter{ $0.pathExtension == "plist" }
        let plistFileNames = plistFiles.map{ $0.deletingPathExtension().lastPathComponent }

        for nameFile in plistFileNames {

            let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let item = (dir as NSString).appendingPathComponent("\(nameFile).plist")

            if item != self.destPath! {

                do {

                    try fileManager.removeItem(atPath: item)
                }

                catch let error as NSError {

                    print("FimeManager Remove Item error: \(error.debugDescription)")
                }
            }
        }

    } catch let error as NSError {

        print("Directory Contents error: \(error.localizedDescription)")
    }
}

I suppose the code can be better, even though it's functional. If anyone has any insights, please share.

Faustena answered 6/2, 2017 at 14:35 Comment(0)
J
0

This will delete all files in the document folder. If the app stores files that you don't want to delete in this directory add code to make sure those files are not deleted.

NSError *errorGettingAllBanners = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] error:&errorGettingAllBanners]) {
    NSError *errorDeletingAllBanners = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:file] error:&errorDeletingAllBanners];
}
Jessamine answered 19/3, 2017 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.