Ubuntu – How to remove older versions of PHP installed via PPA

aptpackage-managementPHPppauninstall

I've seen many similar questions but whatever I tried so far wasn't successful.

I installed PHP in different versions and in parallel from an PPA. This is exactly how I want it.

But now I have some older, unmaintained versions of PHP installed on the system (I even can see e.g. an /etc/php/php5.6 folder with content though php5.6 -v claims it's not installed, but php7.0 -v still works e.g.).

How to get rid of specific older versions and their extensions.

apt purge etc. did not help and I want to keep the PPA for future versions.

EDIT
Looking at the list of installed PHP packages I doubt it's installed from the PPA?

$ apt list --installed php\*
php-cli/xenial,now 2:7.4+72+ubuntu16.04.1+deb.sury.org+1 all  [installiert]
php-common/xenial,now 2:72+ubuntu16.04.1+deb.sury.org+1 all  [Installiert,automatisch]
php-igbinary/xenial,now 3.1.0+2.0.8-2+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php-imagick/xenial,now 3.4.4-1+ubuntu16.04.1+deb.sury.org+20191129 amd64  [installiert]
php-mbstring/xenial,now 2:7.4+72+ubuntu16.04.1+deb.sury.org+1 all  [installiert]
php-pear/xenial,now 1:1.10.8+submodules+notgz-1+ubuntu16.04.1+deb.sury.org+1 all  [installiert]
php-redis/xenial,now 5.1.1+4.3.0-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-cli/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-common/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-curl/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-fpm/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-gd/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-json/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-mbstring/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-mysql/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-opcache/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.0-readline/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.0-xml/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.0-zip/xenial,now 7.0.33-15+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-cli/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.2-common/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.2-curl/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-dev/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-fpm/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-gd/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-intl/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-json/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.2-mbstring/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-mysql/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-opcache/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.2-readline/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.2-xml/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.2-zip/xenial,now 7.2.27-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-cli/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.3-common/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.3-curl/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-gd/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-intl/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-json/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.3-mysql/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-opcache/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.3-readline/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [Installiert,automatisch]
php7.3-xml/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.3-zip/xenial,now 7.3.14-1+ubuntu16.04.1+deb.sury.org+1 amd64  [installiert]
php7.4-cli/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-common/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-curl/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-fpm/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-gd/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-intl/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-json/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-mbstring/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-mysql/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-opcache/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-readline/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [Installiert,automatisch]
php7.4-xml/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]
php7.4-zip/xenial,now 7.4.2-1+ubuntu16.04.1+deb.sury.org+2 amd64  [installiert]

EDIT 2 With list of dpkg (removed 'none's and 'no description available'):

$ dpkg -l php\*
un  php-amqp                                                                   
un  php-apcu                                                                   
un  php-apcu-bc                                                                
un  php-ast                                                                    
un  php-calendar                                                               
ii  php-cli                                2:7.4+72+ubuntu16.04.1+d all                      command-line interpreter for the PHP scripting language (default)
ii  php-common                             2:72+ubuntu16.04.1+deb.s all                      Common files for PHP packages
un  php-console-getopt                                                         
un  php-ctype                                                                  
un  php-curl                                                                   
un  php-dom                                                                    
un  php-exif                                                                   
un  php-ffi                                                                    
un  php-fileinfo                                                               
un  php-fpm                                                                    
un  php-ftp                                                                    
un  php-gd                                                                     
un  php-geoip                                                                  
un  php-gmagick                                                                
un  php-iconv                                                                  
ii  php-igbinary                           3.1.0+2.0.8-2+ubuntu16.0 amd64                    igbinary PHP serializer
ii  php-imagick                            3.4.4-1+ubuntu16.04.1+de amd64                    Provides a wrapper to the ImageMagick library
un  php-intl                                                                   
un  php-json                                                                   
ii  php-mbstring                           2:7.4+72+ubuntu16.04.1+d all                      MBSTRING module for PHP [default]
un  php-mcrypt                                                                 
un  php-memcache                                                               
un  php-memcached                                                              
un  php-mongodb                                                                
un  php-msgpack                                                                
un  php-mysqli                                                                 
un  php-mysqlnd                                                                
un  php-opcache                                                                
un  php-pdo                                                                    
un  php-pdo-mysql                                                              
ii  php-pear                               1:1.10.8+submodules+notg all                      PEAR Base System
un  php-pear-frontend-gtk                                                      
un  php-pear-frontend-web                                                      
un  php-phar                                                                   
un  php-posix                                                                  
un  php-radius                                                                 
un  php-readline                                                               
ii  php-redis                              5.1.1+4.3.0-1+ubuntu16.0 amd64                    PHP extension for interfacing with Redis
un  php-rrd                                                                    
un  php-shmop                                                                  
un  php-simplexml                                                              
un  php-smbclient                                                              
un  php-sockets                                                                
un  php-ssh2                                                                   
un  php-structures-graph                                                       
un  php-sysvmsg                                                                
un  php-sysvsem                                                                
un  php-sysvshm                                                                
un  php-tokenizer                                                              
un  php-uploadprogress                                                         
un  php-uuid                                                                   
un  php-wddx                                                                   
un  php-xdebug                                                                 
un  php-xml                                                                    
un  php-xml-util                                                               
un  php-xmlreader                                                              
un  php-xmlwriter                                                              
un  php-xsl                                                                    
un  php-zip                                                                    
un  php-zmq                                                                    
un  php5-fpm                                                                   
un  php5.6-common                                                              
un  php5.6-igbinary                                                            
un  php5.6-imagick                                                             
un  php5.6-json                                                                
un  php5.6-redis                                                               
un  php7.0-calendar                                                            
ii  php7.0-cli                             7.0.33-15+ubuntu16.04.1+ amd64                    command-line interpreter for the PHP scripting language
ii  php7.0-common                          7.0.33-15+ubuntu16.04.1+ amd64                    documentation, examples and common module for PHP
un  php7.0-ctype                                                               
ii  php7.0-curl                            7.0.33-15+ubuntu16.04.1+ amd64                    CURL module for PHP
un  php7.0-dom                                                                 
un  php7.0-exif                                                                
un  php7.0-fileinfo                                                            
ii  php7.0-fpm                             7.0.33-15+ubuntu16.04.1+ amd64                    server-side, HTML-embedded scripting language (FPM-CGI binary)
un  php7.0-ftp                                                                 
ii  php7.0-gd                              7.0.33-15+ubuntu16.04.1+ amd64                    GD module for PHP
un  php7.0-gettext                                                             
un  php7.0-iconv                                                               
un  php7.0-igbinary                                                            
un  php7.0-imagick                                                             
ii  php7.0-json                            7.0.33-15+ubuntu16.04.1+ amd64                    JSON module for PHP
ii  php7.0-mbstring                        7.0.33-15+ubuntu16.04.1+ amd64                    MBSTRING module for PHP
rc  php7.0-mcrypt                          7.0.33-14+ubuntu16.04.1+ amd64                    libmcrypt module for PHP
ii  php7.0-mysql                           7.0.33-15+ubuntu16.04.1+ amd64                    MySQL module for PHP
un  php7.0-mysqli                                                              
un  php7.0-mysqlnd                                                             
ii  php7.0-opcache                         7.0.33-15+ubuntu16.04.1+ amd64                    Zend OpCache module for PHP
un  php7.0-pdo                                                                 
un  php7.0-pdo-mysql                                                           
un  php7.0-phar                                                                
un  php7.0-posix                                                               
ii  php7.0-readline                        7.0.33-15+ubuntu16.04.1+ amd64                    readline module for PHP
un  php7.0-redis                                                               
un  php7.0-shmop                                                               
un  php7.0-simplexml                                                           
un  php7.0-sockets                                                             
un  php7.0-sysvmsg                                                             
un  php7.0-sysvsem                                                             
un  php7.0-sysvshm                                                             
un  php7.0-tokenizer                                                           
un  php7.0-wddx                                                                
ii  php7.0-xml                             7.0.33-15+ubuntu16.04.1+ amd64                    DOM, SimpleXML, WDDX, XML, and XSL module for PHP
un  php7.0-xmlreader                                                           
un  php7.0-xmlwriter                                                           
un  php7.0-xsl                                                                 
ii  php7.0-zip                             7.0.33-15+ubuntu16.04.1+ amd64                    Zip module for PHP
un  php7.1-igbinary                                                            
un  php7.1-imagick                                                             
un  php7.1-redis                                                               
un  php7.2-calendar                                                            
ii  php7.2-cli                             7.2.27-1+ubuntu16.04.1+d amd64                    command-line interpreter for the PHP scripting language
ii  php7.2-common                          7.2.27-1+ubuntu16.04.1+d amd64                    documentation, examples and common module for PHP
un  php7.2-ctype                                                               
ii  php7.2-curl                            7.2.27-1+ubuntu16.04.1+d amd64                    CURL module for PHP
ii  php7.2-dev                             7.2.27-1+ubuntu16.04.1+d amd64                    Files for PHP7.2 module development
un  php7.2-dom                                                                 
un  php7.2-exif                                                                
un  php7.2-fileinfo                                                            
ii  php7.2-fpm                             7.2.27-1+ubuntu16.04.1+d amd64                    server-side, HTML-embedded scripting language (FPM-CGI binary)
un  php7.2-ftp                                                                 
ii  php7.2-gd                              7.2.27-1+ubuntu16.04.1+d amd64                    GD module for PHP
un  php7.2-gettext                                                             
un  php7.2-iconv                                                               
un  php7.2-igbinary                                                            
un  php7.2-imagick                                                             
ii  php7.2-intl                            7.2.27-1+ubuntu16.04.1+d amd64                    Internationalisation module for PHP
ii  php7.2-json                            7.2.27-1+ubuntu16.04.1+d amd64                    JSON module for PHP
ii  php7.2-mbstring                        7.2.27-1+ubuntu16.04.1+d amd64                    MBSTRING module for PHP
ii  php7.2-mysql                           7.2.27-1+ubuntu16.04.1+d amd64                    MySQL module for PHP
un  php7.2-mysqli                                                              
un  php7.2-mysqlnd                                                             
ii  php7.2-opcache                         7.2.27-1+ubuntu16.04.1+d amd64                    Zend OpCache module for PHP
un  php7.2-pdo                                                                 
un  php7.2-pdo-mysql                                                           
un  php7.2-phar                                                                
un  php7.2-posix                                                               
ii  php7.2-readline                        7.2.27-1+ubuntu16.04.1+d amd64                    readline module for PHP
un  php7.2-redis                                                               
un  php7.2-shmop                                                               
un  php7.2-simplexml                                                           
un  php7.2-sockets                                                             
un  php7.2-sodium                                                              
un  php7.2-sysvmsg                                                             
un  php7.2-sysvsem                                                             
un  php7.2-sysvshm                                                             
un  php7.2-tokenizer                                                           
un  php7.2-wddx                                                                
ii  php7.2-xml                             7.2.27-1+ubuntu16.04.1+d amd64                    DOM, SimpleXML, WDDX, XML, and XSL module for PHP
un  php7.2-xmlreader                                                           
un  php7.2-xmlwriter                                                           
un  php7.2-xsl                                                                 
ii  php7.2-zip                             7.2.27-1+ubuntu16.04.1+d amd64                    Zip module for PHP
un  php7.3-calendar                                                            
ii  php7.3-cli                             7.3.14-1+ubuntu16.04.1+d amd64                    command-line interpreter for the PHP scripting language
ii  php7.3-common                          7.3.14-1+ubuntu16.04.1+d amd64                    documentation, examples and common module for PHP
un  php7.3-ctype                                                               
ii  php7.3-curl                            7.3.14-1+ubuntu16.04.1+d amd64                    CURL module for PHP
un  php7.3-dom                                                                 
un  php7.3-exif                                                                
un  php7.3-fileinfo                                                            
un  php7.3-ftp                                                                 
ii  php7.3-gd                              7.3.14-1+ubuntu16.04.1+d amd64                    GD module for PHP
un  php7.3-gettext                                                             
un  php7.3-iconv                                                               
un  php7.3-igbinary                                                            
un  php7.3-imagick                                                             
ii  php7.3-intl                            7.3.14-1+ubuntu16.04.1+d amd64                    Internationalisation module for PHP
ii  php7.3-json                            7.3.14-1+ubuntu16.04.1+d amd64                    JSON module for PHP
rc  php7.3-mbstring                        7.3.13-1+ubuntu16.04.1+d amd64                    MBSTRING module for PHP
ii  php7.3-mysql                           7.3.14-1+ubuntu16.04.1+d amd64                    MySQL module for PHP
un  php7.3-mysqli                                                              
un  php7.3-mysqlnd                                                             
ii  php7.3-opcache                         7.3.14-1+ubuntu16.04.1+d amd64                    Zend OpCache module for PHP
un  php7.3-pdo                                                                 
un  php7.3-pdo-mysql                                                           
un  php7.3-phar                                                                
un  php7.3-posix                                                               
ii  php7.3-readline                        7.3.14-1+ubuntu16.04.1+d amd64                    readline module for PHP
un  php7.3-redis                                                               
un  php7.3-shmop                                                               
un  php7.3-simplexml                                                           
un  php7.3-sockets                                                             
un  php7.3-sysvmsg                                                             
un  php7.3-sysvsem                                                             
un  php7.3-sysvshm                                                             
un  php7.3-tokenizer                                                           
un  php7.3-wddx                                                                
ii  php7.3-xml                             7.3.14-1+ubuntu16.04.1+d amd64                    DOM, SimpleXML, WDDX, XML, and XSL module for PHP
un  php7.3-xmlreader                                                           
un  php7.3-xmlwriter                                                           
un  php7.3-xsl                                                                 
ii  php7.3-zip                             7.3.14-1+ubuntu16.04.1+d amd64                    Zip module for PHP
un  php7.4-calendar                                                            
ii  php7.4-cli                             7.4.2-1+ubuntu16.04.1+de amd64                    command-line interpreter for the PHP scripting language
ii  php7.4-common                          7.4.2-1+ubuntu16.04.1+de amd64                    documentation, examples and common module for PHP
un  php7.4-ctype                                                               
ii  php7.4-curl                            7.4.2-1+ubuntu16.04.1+de amd64                    CURL module for PHP
un  php7.4-dom                                                                 
un  php7.4-exif                                                                
un  php7.4-ffi                                                                 
un  php7.4-fileinfo                                                            
ii  php7.4-fpm                             7.4.2-1+ubuntu16.04.1+de amd64                    server-side, HTML-embedded scripting language (FPM-CGI binary)
un  php7.4-ftp                                                                 
ii  php7.4-gd                              7.4.2-1+ubuntu16.04.1+de amd64                    GD module for PHP
un  php7.4-gettext                                                             
un  php7.4-iconv                                                               
un  php7.4-igbinary                                                            
un  php7.4-imagick                                                             
ii  php7.4-intl                            7.4.2-1+ubuntu16.04.1+de amd64                    Internationalisation module for PHP
ii  php7.4-json                            7.4.2-1+ubuntu16.04.1+de amd64                    JSON module for PHP
ii  php7.4-mbstring                        7.4.2-1+ubuntu16.04.1+de amd64                    MBSTRING module for PHP
ii  php7.4-mysql                           7.4.2-1+ubuntu16.04.1+de amd64                    MySQL module for PHP
un  php7.4-mysqli                                                              
un  php7.4-mysqlnd                                                             
ii  php7.4-opcache                         7.4.2-1+ubuntu16.04.1+de amd64                    Zend OpCache module for PHP
un  php7.4-pdo                                                                 
un  php7.4-pdo-mysql                                                           
un  php7.4-phar                                                                
un  php7.4-posix                                                               
ii  php7.4-readline                        7.4.2-1+ubuntu16.04.1+de amd64                    readline module for PHP
un  php7.4-redis                                                               
un  php7.4-shmop                                                               
un  php7.4-simplexml                                                           
un  php7.4-sockets                                                             
un  php7.4-sysvmsg                                                             
un  php7.4-sysvsem                                                             
un  php7.4-sysvshm                                                             
un  php7.4-tokenizer                                                           
ii  php7.4-xml                             7.4.2-1+ubuntu16.04.1+de amd64                    DOM, SimpleXML, XML, and XSL module for PHP
un  php7.4-xmlreader                                                           
un  php7.4-xmlwriter                                                           
un  php7.4-xsl                                                                 
ii  php7.4-zip                             7.4.2-1+ubuntu16.04.1+de amd64                    Zip module for PHP
un  phpapi-20131226                                                            
un  phpapi-20151012                                                            
un  phpapi-20160303                                                            
un  phpapi-20170718                                                            
un  phpapi-20180731                                                            
un  phpapi-20190902

Best Answer

PHP 5.6 is no more installed and its configuration files are no more tracked by the package management system APT/dpkg. If it were, dpkg -l would produce a line starting with rc for it. So you can simply rm the remaining tracks of PHP 5.x found on your machine. (Note: Taking a backup before removing files is advisable.)

The versions 7.0, 7.2, 7.3 and 7.4 are installed using APT/dpkg on your machine and you can use the package management system’s tools to uninstall them, e.g. apt purge. The command to remove everything related to PHP 7.0 would be:

sudo apt purge php7.0\*

Note: Check what is going to be removed before proceeding.

Related Question