How do I remove all packages from a certain repository?

For a smooth upgrade from 10.04 to 10.10, I would like to remove all packages which are not in the default repositories (e.g. chromium PPA and third-party repositories). What is the easiest way (preferably CLI) to find and remove these packages?

Edit: this question is not restricted to PPA's, I have a x2go repository as well, so I'm looking for a generic command to remove packages belonging to a certain repository. For PPA's, the question How can PPAs be removed has great answers.


Check out ppa-purge Install ppa-purge.

Usage:

ppa-purge -purge <nameofppa> [-s hostserver]

Hostserver defaults to ppa.launchpad.net.

Here is the link to the manpage for the ppa-purge command Manpage icon for reference.


Overcoming aptitude's Multiarch Bug for ppa-purge

Normally, as Vadim Rutkovsky and Takkat have explained, running ppa-purge with default syntax is an effective way to remove all packages provided by a PPA, properly downgrading them to versions provided in other configured software sources whenever possible.

However, as Takkat has pointed out, bug 831768 prevents ppa-purge from working properly to remove multiarch packages (like 32-bit packages installed on 64-bit systems). This is because aptitude cannot handle conflicting dependencies in multiarch packages (that's what the bug is about), and the default behavior of ppa-purge is to use aptitude to downgrade packages.

Fortunately, ppa-purge accepts the -i flag which causes it to prefer apt-get to aptitude as its backend. As documented in the bug report, using apt-get is an effective alternative to manually invoking aptitude, so running ppa-purge with the -i flag should be an effective workaround for removing/downgrading all packages provided by a PPA, including multiarch packages.

How To Do It

Here's the syntax:

sudo ppa-purge -i ppa:ppaowner/ppaname

As usual (same as when the -i flag is not used):

  • ppaowner is replaced with the owner of the PPA.
  • /ppaname is optional. If present, ppaname is replaced with the name of the PPA. If not, it defaults to ppa.

For example, suppose I had the PPA for unstable builds of qBittorrent installed, and I wanted to remove it completely, automatically downgrading qBittorrent to whatever version is available through my other software sources using apt-get behind the scenes instead of aptitude. Then I would run:

sudo ppa-purge -i ppa:hydr0g3n/qbittorrent-unstable

Where It's Documented

For some reason, a few useful options for ppa-purge are not documented in its manual page, including -i. But you can get information about them by running ppa-purge -h (or just ppa-purge with no arguments):

ek@Del:~$ ppa-purge -h
Usage: sudo ppa-purge [options] <ppa:ppaowner>[/ppaname]

ppa-purge will reset all packages from a PPA to the standard
versions released for your distribution.

Options:
    -p [ppaname]        PPA name to be disabled (default: ppa)
    -s [host]       Repository server (default: ppa.launchpad.net)
    -d [distribution]   Override the default distribution choice.
    -y          Pass -y --force-yes to apt-get or -y to aptitude
    -i          Reverse preference of apt-get upon aptitude.
    -h          Display this help text

Example usage commands:
    sudo ppa-purge xorg-edgers
    will remove https://launchpad.net/~xorg-edgers/+archive/ppa

    sudo ppa-purge -p xorg-testing sarvatt
    will remove https://launchpad.net/~sarvatt/+archive/xorg-testing

    sudo ppa-purge ppa:ubuntu-x-swat/x-updates
    will remove https://launchpad.net/~ubuntu-x-swat/+archive/x-updates

Notice: If ppa-purge fails for some reason and you wish to try again,
(For example: you left synaptic open while attempting to run it) simply
uncomment the PPA from your sources, run apt-get update and try again.

"Reverse preference of apt-get upon aptitude" is a bit cryptic. By looking at the relevant piece of the source code (ppa-purge is just a shell script, after all), we can see that this means that the default behavior is to prefer aptitude to apt-get, and the -i flag reverses this preference.