Skip to main content

How to delete albums

You can delete albums with the deleteAssetCollections API, for example:

import Photos

func deleteAlbum(_ album: PHAssetCollection) -> Void {
    try! PHPhotoLibrary.shared().performChangesAndWait({
        PHAssetCollectionChangeRequest
            .deleteAssetCollections([album] as NSFastEnumeration)
    })
}

There are lots of ways to find instances of PHAssetCollection to pass to this function; for example, you could use fetchTopLevelUserCollections and filter for albums with a particular name or which are empty:

import Photos

PHCollectionList
    .fetchTopLevelUserCollections(with: PHFetchOptions())
    .enumerateObjects({ (collection, index, stop) in
        if collection is PHAssetCollection {
            let album = collection as! PHAssetCollection

            if album.localizedTitle == "Empty" && album.estimatedAssetCount == 0 {
                print(album)
                deleteAlbum(album)
            }
        }
    })