If you use munki to serve software to your Macs, and autopkg to automatically fetch new updates, you might end up with a bit of an oversized munki-repo after a while.

This could be due to some automation gone wrong downloading duplicate pkgs without adding pkginfo-files. Or it could be a colleague who hasn’t had their coffee yet munkiimporting something without committing the pkginfo-file or it could be a lot of things!

For these cases you could use a simple Python-script for traversing the pkgs-directory, and checking every pkginfo-file for whether or not that pkg is used anywhere. Such a script could look something like this.

#!/usr/bin/env python
  ./find_orphaned_pkgs.py | grep -ie libreoffice | xargs rm # Remove all orphaned libreoffice pkgs
  ./find_orphaned_pkgs.py --munki-repo path/to/munki/repo
  ./find_orphaned_pkgs.py --count
import argparse
import os.path as op
import os
import re
import plistlib

def munki_path(full_path):
    return re.search(r'^.+pkgs\/(.+)$', full_path).group(1)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-m', '--munki-repo', type=str, default='./',
                        help='Path to munki repo. Defaults to current working dir.')
    parser.add_argument('-c', '--count', action='store_true',
                        help='Print total number of orphans.')

    args = parser.parse_args()

    installer_item_location = 'installer_item_location'

    if not op.exists(args.munki_repo):
        raise Exception('No such path')

    dirs = os.listdir(args.munki_repo)

    if not 'pkgs' in dirs and not 'pkgsinfo' in dirs:
        raise Exception('This does not look like a munki repo')

    pkgs = []

    for root, dirs, files in os.walk(op.join(args.munki_repo, 'pkgs/')):
        for _dir in dirs:
            if _dir.endswith('.pkg'):
                pkgs.append(munki_path(op.join(root, _dir)))

        for _file in files:
            if _file.startswith('._') or _file.startswith('.DS_Store'):

            path = munki_path(op.join(root, _file))

            # Don't react on pkgs where the script is able to traverse sub-files
            if '.pkg' in path and (not path.endswith('.pkg') or path.count('.pkg') != 1):


    installer_items = []

    for root, dirs, files in os.walk(op.join(args.munki_repo, 'pkgsinfo')):
        for pkginfo in files:
            plist = plistlib.readPlist(op.join(root, pkginfo))

            if not installer_item_location in plist.keys():


    counter = 0

    for pkg in pkgs:
        if pkg not in installer_items:
            counter += 1
            print op.join(args.munki_repo, 'pkgs', pkg)

    if args.count:
        print 'Found %d orphaned files.' % counter

This can easily be used together with other unix-tools like grep and rm to filter and delete the pkgs you want.

$ ./find_orphaned_pkgs.py | grep -ie spotify

$ ./find_orphaned_pkgs.py | grep -ie spotify | xargs rm

$ ./find_orphaned_pkgs.py | grep -ie spotify