Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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

In my local repository see below, I have many versions of same packages downloaded and stored in deb package directory. As Ubuntu always prefer to use the highest version, I don't need the earlier versions downloaded and used. (All are in a same folder).

How can I delete the earlier versions of packages from my local deb's folder ? It is not a feasible solution to delete them manually. (You can't examine the versions in thousands of files)

Any ideas?!!

Maybe a script that checks the version of each package keeping the newer version and deletind older ones can solve this issue.

I am seeking for such script or if some software is founded would be great

I have made my local repository following this how to tutorial.

*** A local repository is an offline storage of packages already downloaded, so that you don't need to download it again)

Note: I'm not referring to clearing the cache of downloaded packages, which can be done with apt-get clean command.

share|improve this question
+1 for the question. I also have a local repository and i delete my older versions with some tedious method (semi-automatic script) – Anwar Shah Oct 10 '12 at 9:39
Thanks..Could you show me that script?! please – user61928 Oct 10 '12 at 10:11
@OliveTwist I, too, would love to see the script. – hexafraction Oct 10 '12 at 12:42
@ObsessiveSSOℲ I've given the answer. you can check. – Anwar Shah Oct 11 '12 at 5:23
up vote 5 down vote accepted

I've deleted the old versions of same packages following this. I used the inversion of the feature of dpkg-scanpackages's multiple version scanning feature.

  1. First install dpkg-dev package

    sudo apt-get install dpkg-dev
  2. Then generate a file with the name of packages (only newer will be listed) dpkg-scanpackages without the -m option. The default is without -m option.

    If you have .deb files in a folder named deb, run the below command from the parent of this folder

    dpkg-scanpackages deb /dev/null 2>/dev/null | grep Filename: > filenames

    This will create a file with name filenames which have all the .deb files' name listed in a format Filename: deb/packagename_version.deb.

    We now have all the names of files with newest versions in a file named filenames

  3. The task is now simple, modify the script to move all those files in another folder.

    1. First replace the Filename: with mv

      sed -i 's/Filename:/mv/' filenames
    2. Now create a folder in the parent directory of deb folder. I named it newest (junk-free could be a good one ;P).

    3. Again change the filenames file to move the .deb files in the newly created newest folder.

      sed -i 's/\.deb/.deb newest/' filenames

      This will make our filenames file a list of mv commands moving .deb files from deb directory to newest directory

    4. Now guess what. Execute the file filenames

      sh filenames
    5. The last step is delete the folder with older obsolete .deb files. Check the newest folder too as a pre-caution.

Update with one liner

After installing dpkg-dev packages, generate the move script with a single command, use this one by going to the parent of the .deb files' folder.

 dpkg-scanpackages deb /dev/null 2>/dev/null | grep Filename: | sed 's/Filename:/mv/;s/\.deb/.deb newest/' > filenames

Then create a folder named newest and execute the file filenames with `sh filenames* command.

share|improve this answer
+1.nice script. leyt me check and i'll provide my results – user61928 Oct 11 '12 at 5:33
It works perfect. thanks – user61928 Oct 11 '12 at 5:51

Update with one liner To Delete (not to move)

this one liner will delete only old versions of same packages

run it as root

sudo dpkg-scanpackages /var/cache/apt/archives 2>&1 >/dev/null | grep -Po '((\/.*?deb)(?=.*?repeat;))|used that.*?\K(\/.*deb)' | xargs rm

"/var/cache/apt/archives" replace with your .deb directory

it works very good in Ubuntu 12.10

share|improve this answer

I had the same problem: I created a local repository using apt-mirror and this tool downloads new packages but leaves all the old packages in place. I did the following to cleanup my local mirror:

  1. Go to the location of your top of your mirror (the directory where the dists and pool directory are located, in my setup /var/spool/apt-mirror/mirror/

  2. Create a list of all packages in your pool:

    find pool -type f | sort -u > files
  3. Create a list of all packages in your mirrored distribution:

    grep "^Filename:" `find dists/ -name Packages`|sed "s/^.*Filename: //"|sort -u > packages
  4. Now create a diff between the two files and convert it into a cleanup script:

    diff files packages | sed "/^[1-9]/d;s/^< /rm /" >
  5. Execute the generated script

Make sure you have write-permissions on the repository (or execute using sudo). Instead of removing you can also move the files to a temporary location.

The idea behind this is that you can remove the files that are not mentioned in one of the Packages files, as they are replaced with newer versions or are deprecated. If files are not mentioned in the Packages files they cannot be found by clients connecting to your repository.

share|improve this answer
I updated your last statement to also remove --- and > lines found in the diff; diff files packages | sed "/^[1-9]/d;/^---/d;/^>/d;s/^< /rm /" > – Kolky Apr 1 at 9:54

Nice workaround from "Anwar" (in the first answer)! Only this line I would change:

sed -i 's/\.deb/.deb newest/' filenames

because if you have a Package called: packagename.debian_123.deb your line will only replace the first ".deb" in this line and that would look like this in the end:

packagename.deb newest_123.deb

Take this line instead, and only the last .deb in every line will be replaced:

sed -i 's/\(.*\).deb/\1.deb newest/' filenames
share|improve this answer

In Thunar:

  1. Open /var/cache/apt/archives
  2. Select files based on access time:
    • click View >> View as Detailed List
    • click View >> Configure Columns >> enable Date Accessed
    • click on the Date Accessed table header, to sort the files
    • ctrl-click to select multiple files, shift-click to select a range of files
  3. Delete the files, either via right-click >> Delete (if Thunar was opened with sufficient privileges) or:
    • Right click >> choose Rename
    • choose Insert/Overwrite, set a custom prefix
    • open a terminal, sudo rm /var/cache/apt/archives/customprefix*.deb
share|improve this answer

use software center and search Computer-Janitor and Ubuntu tweak. INSTALL and run. Go to system and select apt cache or old packages and clean

share|improve this answer

Your Answer


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