def check_items(items, expiry):
    """
    Check for old deployments that have files that can be deleted
    and prune old deployments from the item list.

    The algorithm finds all deployments older than the expiry
    and keeps only the newest of them.

    Returns a set containing the remaining items and a list of
    files to be deleted.
    """

    deployments = list(set([i.time for i in items]))
    deployments_old = sorted([i for i in deployments if i < expiry])

    if len(deployments_old) <= 1:
        return items, []

    delete_older_than = deployments_old[-1]

    keep_files = list(set([i.filename for i in items if i.time >= delete_older_than]))

    delete_files = list(
        set(
            [
                i.filename
                for i in items
                if i.time < delete_older_than and i.filename not in keep_files
            ]
        )
    )

    return [i for i in items if i.time >= delete_older_than], delete_files