Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know a way to clean the var/cache/apt folder in a way that it only leaves the latest version of a package if it has several versions or it leaves a package if it is the only one of that program.

For example I have several vlc packages (vlc_1.1.11, vlc_1.1.12..) and several wine packages (wine1.3_1.3.34,wine1.3_1.3.35,wine1.3_1.3.36,wine1.3_1.3.37...) and many others like this.

So how to do a clean up in this folder that it leaves only the latest packages. At the moment I have 2.5GB and most of it are just older packages mixed with the newer ones.

share|improve this question
up vote 8 down vote accepted

I propose the following bash script


cd /var/cache/apt/archives/
printf '%s\n' *.deb | 
  awk -F_ '{ print $1 }' |
  sort -u | 
  while read pkg; do 
    pkg_files=($(ls -t "$pkg"_*.deb))
    if ((nr > 1)); then
      unset pkg_files[0]
      echo rm "${pkg_files[@]}"

Remove the echo from the rm line if you are satisfied with the output list.

What it does?

  1. It list all deb package files
  2. remove everything in the filename from the first "_" to the end, obtaining the package name
  3. sort the names, removing duplicates
  4. for each name

    1. list the package files corresponding to that name in time order
    2. count the number of package files in the list
    3. if there is more than one package in the list

      1. remove from the list the first and newer file
      2. remove from the disk all other files corresponding to that name

It could be improved in efficiency, by listing only package files corresponding to package names obtained from the difference between sort and sort -u.

share|improve this answer
It worked perfectly. I am also giving a +1 to bodhi because autoclean did also help in cleaning up a bit. So maybe both together do a great work. Thanks enzotib. – Luis Alvarado Jan 18 '12 at 21:21

use the autoclean option to apt-get or aptitude

sudo apt-get autoclean
sudo aptitude autoclean

From the man page


clean clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.


Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control.

share|improve this answer
Can you add sudo aptitude autoclean. Also the autoclean in both cases (apt-get and aptitude) still leaves older versions that make a huge jump like thunderbird 8.0 and thundebird 9.0. It did however remove the thinderbird 8.x older packages but it leaves like the full release versions without taking into consideration that 9.0 is above 8.0. This is just one of many cases I still have left. Any other ideas to face this amount of older packages? – Luis Alvarado Jan 18 '12 at 17:57
I am sorry, if autoclean did not work I do not know of a simple method. Hope someone else has a better suggestion. – bodhi.zazen Jan 18 '12 at 19:31

I propose a simpler and more human readable bash script, based on enzotib's algorithm, which doesn't remove extra files, like the original do:

#! /usr/bin/env bash

cd /var/cache/apt/archives/
for pkg in `ls *.deb | cut -d _ -f 1 | sort -u`; do
    if [ $(ls $pkg\_* | wc -l) -gt 1 ]; then
        files=$(ls -vr $pkg\_*)
        rmfiles=`echo $files | cut -d " " -f 2-`
        rm -v $rmfiles
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.