Here's a function to delete a custom album programmatically with error handling:
func deleteAlbum(albumName: String) {
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "title = %@", albumName)
let album = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
// check if album is available
if album.firstObject != nil {
// request to delete album
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.deleteAssetCollections(album)
}, completionHandler: { (success, error) in
if success {
print(" \(albumName) removed succesfully")
} else if error != nil {
print("request failed. please try again")
}
})
} else {
print("requested album \(albumName) not found in photos")
}
}
How to use -
deleteAlbum(albumName: "YourAlbumName")