How to delete old versions of same packages from a Local Repository?

Solution 1:

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.

Solution 2:

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

Solution 3:

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/ftp.nl.debian.org/debian)

  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 /" > cleanup.sh
    
  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.