Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Remove Ubuntu Packages Correctly

By Kokil Thapa | Last reviewed: September 2026

You need to remove Ubuntu packages correctly when a dev tool, old PHP version, or unused desktop app clutters a server you maintain for Laravel, WordPress, or eCommerce workloads. A careless apt remove can leave broken configs, orphan libraries, or a half-deleted stack that fails on the next deploy. This guide walks through the safe removal workflow I use on production Ubuntu 22.04 and 24.04 hosts — from inspection through purge, autoremove, and verification — so you free disk space without breaking what still runs.

How do you inspect installed Ubuntu packages before removal?

Never uninstall blind on a production box. Start by identifying the exact package name, not the command you type in the shell. On a real client project hosting a PHP-FPM stack on Ubuntu, the binary might be php8.4-fpm while related packages include php8.4-cli, php8.4-mysql, and php8.4-xml.

List installed packages and filter by keyword:

apt list --installed 2>/dev/null | grep -i nginx
dpkg -l | grep -i mysql
dpkg -S /usr/bin/composer

The first command queries APT indexes. The second shows package state codes in the left column: ii means installed and configured, rc means removed but config files remain, and un means unknown or broken.

Find reverse dependencies — packages that depend on what you plan to remove:

apt-cache rdepends nginx
apt-cache rdepends php8.3-fpm

If your Laravel app, Redis, or MySQL client depends on the target package, removal will break the stack. For web servers I maintain under Linux system administration, I also check running services before touching packages:

systemctl list-units --type=service --state=running
ss -tlnp

Document what you remove. A one-line note in your deploy log saves hours when a cron job fails two weeks later because a library vanished.

Remove Ubuntu Packages Correctly1. Inspectdpkg -l, rdepends2. Removeapt remove3. Purge--purge configs4. CleanautoremoveNever skip on production serversCheck rdepends before removeBack up /etc before purgeVerify services after autoremoveTest deploy path and cron jobs
Four-step workflow to remove Ubuntu packages correctly on dev and production hosts

What is the difference between apt remove, purge, and autoremove?

APT gives you three distinct actions. Treat them as separate decisions, not one bulk command on a live server.

CommandRemoves binariesRemoves config filesTypical use
apt remove pkgYesNoTemporary uninstall; you may reinstall later
apt purge pkgYesYesFull cleanup of app and its /etc settings
apt autoremoveOrphans onlyOptional with --purgeRemoves libs nothing else needs
apt remove --purge pkgYesYesSingle-step full removal (most common)

The official Ubuntu apt manual documents these flags. In practice, remove leaves config stubs under /etc. That is fine for desktop trials. On a web server, stale nginx or Apache configs cause confusion during the next Nginx install.

Remove a single package

sudo apt remove apache2
sudo apt remove --purge apache2

The first keeps /etc/apache2/. The second deletes it. For PHP version switches — common when upgrading from PHP 8.3 to 8.4 on Laravel 13 — purge old FPM pools so systemd does not reference dead socket paths.

sudo apt remove --purge php8.2-fpm php8.2-cli php8.2-common \
  php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl

Group packages by version. Mixing 8.2 and 8.4 in one command is a common mistake that pulls the wrong dependency set.

Autoremove orphaned dependencies

sudo apt autoremove
sudo apt autoremove --purge

Autoremove deletes packages installed only as dependencies and no longer required. Read the prompt list carefully. I have seen autoremove propose removing a library still loaded by a manually compiled binary. When in doubt, skip autoremove until you verify.

apt remove vs purge vs autoremoveapt removeBinaries: gone/etc configs: keptState: rc possibleapt purgeBinaries: gone/etc configs: goneClean reinstallautoremoveOrphan libs onlyNeeds reviewAfter main removeRecommended production sequenceremove --purge target packagesautoremove --purge (review list)apt clean & verify services
How apt remove, purge, and autoremove affect binaries and configuration on Ubuntu

How do you remove Ubuntu packages correctly on a production web server?

Production removal differs from desktop cleanup. You share the box with running services, cron jobs, and deploy scripts. I follow this ordered checklist on Ubuntu servers that host Laravel apps, WooCommerce stores, and legal-tech portals.

  1. Take a snapshot or backup /etc and the database before major removals.
  2. Put the app in maintenance mode or drain traffic if you remove the web stack.
  3. Stop the service tied to the package: sudo systemctl stop nginx.
  4. Run apt remove --purge on the target packages.
  5. Run apt autoremove --purge only after reviewing the candidate list.
  6. Reload systemd: sudo systemctl daemon-reload.
  7. Verify ports, PHP-FPM pools, and cron paths still resolve.
  8. Run your deploy smoke test or hit a health-check URL.

For sites on shared EC2 infrastructure — the same Deployer 7 pipeline I use for sister legal-tech domains — a broken PHP extension shows up only after symlink swap. Test php -m and a queue worker before you close the ticket.

Hold packages you must not upgrade or remove

Sometimes you need an older MySQL client or a pinned PHP build. Mark it on hold:

sudo apt-mark hold php8.3-fpm
apt-mark showhold

Release the hold when you are ready to migrate:

sudo apt-mark unhold php8.3-fpm

Held packages block apt upgrade from touching them. That is useful during a staged PHP migration documented in your safe apt upgrade workflow.

Clean leftover package state with dpkg

When APT reports broken packages, dpkg is the lower-level tool. The dpkg reference covers these recovery commands:

sudo dpkg --configure -a
sudo apt --fix-broken install
sudo dpkg --purge packagename

Packages in rc state still occupy dpkg metadata. Purge them explicitly:

dpkg -l | awk '/^rc/ { print $2 }' | xargs sudo dpkg --purge

Run that only after you confirm none of those configs hold secrets you still need.

Remove packages installed from third-party repositories

PPAs and vendor repos — Ondřej Surý PHP, NodeSource, Docker — need repo cleanup after package removal. Otherwise apt update keeps fetching dead metadata. See Ubuntu repository management for the full pattern:

sudo add-apt-repository --remove ppa:ondrej/php
sudo rm /etc/apt/sources.list.d/old-list-file.list
sudo apt update

On a server running MySQL 8.4 LTS alongside a Laravel app, removing the Oracle MySQL repo without purging packages first leaves broken apt sources. Remove packages, then delete the list file, then update.

How do you remove Snap packages and other non-APT software on Ubuntu?

Modern Ubuntu desktop and some cloud images ship Snaps by default. APT commands do not touch them. If you followed our Snap packages guide, you already know they live in a separate namespace.

snap list
sudo snap remove firefox
sudo snap remove --purge chromium

The --purge flag on snap removes user data under ~/snap/. On servers, Snaps are less common but Docker and certbot sometimes arrive as snaps on desktop-derived installs.

For software installed outside APT entirely — Composer global binaries, Node via nvm, manual Go builds — removal is manual:

  • Delete the binary from /usr/local/bin or your home directory.
  • Remove systemd unit files under /etc/systemd/system/.
  • Search cron with crontab -l and /etc/cron.d/.
  • Grep deploy scripts for hard-coded paths.

I have fixed production cron failures where an old Node 18 path lingered after switching to Node.js 26 LTS via nvm. Path audits matter as much as package removal.

Three removal paths on UbuntuAPT / dpkgapt remove --purgeSnapsnap remove --purgeManualDelete + systemdnginx, php-fpm, mysqlfirefox, chromium, lxdnvm node, composerWrong tool = package still present or broken stateAlways match removal command to install methodDocument install source in server runbook
Match your uninstall command to how the software was originally installed on Ubuntu

What cleanup commands should you run after removing Ubuntu packages?

Removal is half the job. Caches and stale indexes can hide hundreds of megabytes on small VPS plans common for Nepal SMB sites — often Rs 1,500–3,000/month (~USD 11–22).

sudo apt clean
sudo apt autoclean
sudo apt update

apt clean wipes downloaded .deb files from /var/cache/apt/archives/. autoclean removes outdated cached packages only. Neither removes installed software.

Find large leftover directories APT does not track:

sudo du -sh /var/log/* | sort -hr | head -20
sudo du -sh /var/lib/mysql /var/www /home/*

Removing the mysql-server package does not delete your data directory at /var/lib/mysql by default. That is intentional — reinstall can recover data. If you truly want data gone, back up first, then remove the directory manually after purge.

For log rotation after removing a heavy debug tool like Xdebug from a staging server:

sudo journalctl --vacuum-time=7d
sudo find /var/log -type f -name "*.gz" -delete

Pair disk cleanup with monitoring. Our Ubuntu server monitoring guide covers alerts when /var crosses 85% capacity — a frequent trigger for package audits.

Verify nothing broke after removal

Run a focused post-removal checklist:

sudo apt check
php -v
php -m | grep -i mysql
sudo nginx -t
sudo systemctl status php8.4-fpm nginx mysql
curl -I https://your-domain.example

apt check confirms dependency integrity. Web stack tests catch missing modules that autoremove stripped quietly.

Common removal gotchasautoremove kills libPHP ext still referencedpurge deletes /etcCustom vhost lostcron stale path/usr/bin/php8.2 gonedata dir remains/var/lib/mysql intactPrevention checklistBackup /etc — test php -m — grep cron pathsReview autoremove list — run apt check
Production gotchas when you remove Ubuntu packages from PHP, Nginx, and MySQL stacks

How do you safely remove desktop packages without breaking a dual-role Ubuntu server?

Some Nepal agency boxes double as developer workstations and staging hosts. Removing GNOME libraries to save RAM can break update-manager or polkit helpers you still need locally.

Identify metapackages before you prune:

apt-cache depends ubuntu-desktop-minimal | head -30
apt-mark showmanual | grep -i gnome

Remove specific apps, not entire desktop metapackages, unless you intend a headless conversion:

sudo apt remove --purge libreoffice*
sudo apt remove --purge thunderbird

For headless server conversion, follow a staged approach aligned with Ubuntu server setup docs rather than ripping out ubuntu-desktop in one shot. I prefer fresh Ubuntu Server reinstall for production — less drift, cleaner firewall profile under UFW configuration.

Projects like Adventure Third Pole Trek run on lean Laravel stacks where every installed package is attack surface. Periodic audits — quarterly on shared hosting — keep the package list aligned with what the app actually needs.

Key Takeaways

  • Inspect with dpkg -l and apt-cache rdepends before any production removal.
  • Use apt remove --purge for full uninstall including /etc configs on web servers.
  • Review the autoremove candidate list line by line — never accept it blindly on live boxes.
  • Match uninstall tools to install method: APT for deb packages, snap remove for Snaps, manual cleanup for nvm and Composer globals.
  • Back up /etc, run apt check, and verify PHP-FPM, Nginx, and cron paths after every removal session.
  • Remove orphaned PPAs and repo list files after purging third-party packages to keep apt update clean.

People Also Ask

Does apt remove delete configuration files?

No. Standard apt remove uninstalls program binaries but keeps configuration files under /etc. Use apt purge or apt remove --purge to delete those configs. Leftover configs show as rc state in dpkg -l output.

Is apt autoremove safe to run?

Usually yes on desktop systems. On production servers, it can remove libraries still needed by custom binaries or older PHP extensions. Always read the package list APT proposes before confirming. Run apt check and service tests immediately after.

How do I completely uninstall MySQL from Ubuntu?

Stop MySQL, then run sudo apt remove --purge mysql-server mysql-client mysql-common. Follow with sudo apt autoremove --purge. Data under /var/lib/mysql may remain until you delete it manually after a verified backup. Remove MySQL APT repo files if you added Oracle's repository.

What is the difference between apt remove and apt purge?

apt remove deletes the application binaries only. apt purge deletes binaries plus configuration files shipped by the package. For clean reinstalls — switching from Apache to Nginx, or changing PHP versions — purge is the correct choice.

Remove Ubuntu packages correctly and keep your stack stable

Package removal is maintenance, not housekeeping. Done right, it frees disk, shrinks attack surface, and clears stale configs that confuse the next upgrade. Done wrong, it breaks PHP extensions, kills cron paths, or leaves apt in a broken state mid-deploy. The pattern is simple: inspect dependencies, purge intentionally, autoremove cautiously, then verify services and run apt check before you walk away.

If you maintain production Ubuntu hosts for Laravel, WordPress, or booking platforms and want a second pair of eyes before major package changes, see our support and maintenance service or review related guides on installing packages with apt, MySQL on Ubuntu, and server hardening. For quick config sanity checks while editing JSON deploy manifests, the JSON formatter tool on our site helps catch syntax errors before they hit CI. When you are ready for hands-on help auditing a live VPS, contact us — we remove Ubuntu packages correctly without taking your app offline longer than necessary.

Frequently Asked Questions

Use sudo apt remove --purge packagename for full uninstall including configs, then sudo apt autoremove --purge for orphaned dependencies. Always inspect with apt list --installed and dpkg -l before deleting anything on a live host.

No. Standard apt remove uninstalls program binaries but keeps configuration files under /etc. Use apt purge or apt remove --purge to delete those configs. Leftover configs show as rc state in dpkg -l output.

apt remove deletes application binaries only. apt purge deletes binaries plus configuration files shipped by the package. For clean reinstalls, such as switching from Apache to Nginx or changing PHP versions, purge is the correct choice.

Never uninstall blind on a production box. Identify exact package names with apt list --installed filtered by keyword and dpkg -l for state codes: ii means installed, rc means removed but config remains, un means unknown or broken. Use dpkg -S to find which package owns a binary path. Check reverse dependencies with apt-cache rdepends so you do not break Laravel, Redis, or MySQL clients. On web servers I maintain, I also list running services with systemctl and open ports with ss before touching packages. Document what you remove in your deploy log.

APT gives you three distinct actions, not one bulk command on a live server. apt remove deletes binaries but leaves /etc configs, useful for temporary uninstalls. apt purge removes binaries and config files, the right choice when stale nginx or Apache configs would confuse the next install. apt autoremove deletes packages installed only as dependencies and no longer required. apt remove --purge combines full removal in one step and is the most common pattern on web servers. On PHP version switches, purge old FPM pools so systemd does not reference dead socket paths.

Usually yes on desktop systems, but production is different. autoremove can propose removing libraries still loaded by a manually compiled binary or quietly strip PHP extensions your Laravel app needs. Always read the package list APT shows before confirming. When in doubt, skip autoremove until you verify nothing critical depends on the candidates. Run sudo apt check immediately after, then test php -m, nginx -t, and queue workers. On shared EC2 hosts using Deployer 7, a missing extension often surfaces only after symlink swap, not during the removal itself.

Production removal shares the box with running services, cron jobs, and deploy scripts. Take a snapshot or backup /etc and the database before major removals. Put the app in maintenance mode or drain traffic if you remove the web stack. Stop the tied service first, such as sudo systemctl stop nginx. Run apt remove --purge on target packages, then autoremove --purge only after reviewing the candidate list. Reload systemd with daemon-reload, verify ports, PHP-FPM pools, and cron paths still resolve, then run a deploy smoke test or health-check URL before you close the ticket.

Stop MySQL first, then run sudo apt remove --purge mysql-server mysql-client mysql-common, followed by sudo apt autoremove --purge. Data under /var/lib/mysql may remain until you delete it manually after a verified backup; that is intentional so reinstall can recover data. If you added Oracle's MySQL repository, remove the APT repo list files after purging packages, then run apt update. Removing the repo before purging packages leaves broken apt sources, a pattern I have seen on Laravel hosts running MySQL 8.4 LTS alongside application code.

Use sudo apt-mark hold packagename to block apt upgrade from touching a package you must keep pinned, such as an older PHP build or MySQL client during a staged migration. View currently held packages with apt-mark showhold. Release the hold when you are ready to migrate with sudo apt-mark unhold packagename. Held packages are useful when upgrading from PHP 8.3 to 8.4 on Laravel 13 and you need the old FPM stack alive until the new pool is verified. Document holds in your deploy notes so the next upgrade session does not surprise you.

When APT reports broken packages, drop to the lower-level dpkg recovery commands: sudo dpkg --configure -a, then sudo apt --fix-broken install, and sudo dpkg --purge packagename for stubborn entries. Packages in rc state still occupy dpkg metadata even though binaries are gone; purge them explicitly with dpkg -l piped through awk to xargs dpkg --purge. Run that only after confirming none of those leftover configs hold secrets you still need. I use this cleanup routinely before running apt check on servers that accumulated half-removed stacks from rushed desktop-style uninstalls.

Match uninstall to install method. For Ondřej Surý PHP, NodeSource, or Docker repos, remove the packages first, then clean the repository metadata. Use sudo add-apt-repository --remove ppa:ondrej/php or delete stale list files under /etc/apt/sources.list.d/, then sudo apt update. On a server running MySQL 8.4 LTS with a Laravel app, removing the Oracle MySQL repo without purging packages first leaves broken apt sources that break every subsequent apt update. Order matters: purge packages, delete the list file, then update indexes.

APT commands do not touch Snaps. They live in a separate namespace from deb packages. List installed snaps with snap list, then remove with sudo snap remove packagename or sudo snap remove --purge packagename to also delete user data under ~/snap/. Snaps are less common on servers but Docker and certbot sometimes arrive as snaps on desktop-derived cloud images. If you followed a Snap install guide for desktop software, do not expect apt remove to uninstall it. Always verify with snap list after purging related deb packages so nothing orphaned keeps running.

Removal is half the job. Run sudo apt clean to wipe downloaded .deb files from /var/cache/apt/archives/, sudo apt autoclean for outdated cache entries only, and sudo apt update to refresh indexes. Neither clean nor autoclean removes installed software. Find large leftover directories APT does not track with du on /var/log, /var/lib/mysql, and /var/www. On small VPS plans common for Nepal SMB sites, often Rs 1,500–3,000/month (~USD 11–22), this frees hundreds of megabytes. Vacuum old journals with journalctl --vacuum-time=7d after removing heavy debug tools like Xdebug from staging.

Run a focused post-removal checklist before you walk away. Start with sudo apt check to confirm dependency integrity. Test php -v and php -m for required extensions like mysql, run sudo nginx -t, check systemctl status on php8.4-fpm, nginx, and mysql, then curl your health-check URL. Web stack tests catch missing modules that autoremove stripped quietly. On production Laravel hosts, I also confirm cron paths and queue workers still resolve binaries correctly. Broken removal often shows up days later in cron, not during the uninstall session itself.

Some agency boxes double as developer workstations and staging hosts. Removing GNOME libraries to save RAM can break update-manager or polkit helpers you still need locally. Identify metapackages first with apt-cache depends ubuntu-desktop-minimal and apt-mark showmanual filtered by gnome. Remove specific apps like libreoffice* or thunderbird, not entire desktop metapackages, unless you intend a full headless conversion aligned with Ubuntu server setup docs. For production, I prefer a fresh Ubuntu Server reinstall over ripping out ubuntu-desktop in one shot. Lean Laravel stacks benefit from quarterly package audits that keep every installed package tied to actual app needs.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: