
September 11, 2026
11 min read
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.
sudo apt remove --purge packagename for config-inclusive uninstall, then sudo apt autoremove --purge for orphaned dependencies. Always inspect with apt list --installed and dpkg -l before you delete anything on a live server.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.
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.
| Command | Removes binaries | Removes config files | Typical use |
|---|---|---|---|
apt remove pkg | Yes | No | Temporary uninstall; you may reinstall later |
apt purge pkg | Yes | Yes | Full cleanup of app and its /etc settings |
apt autoremove | Orphans only | Optional with --purge | Removes libs nothing else needs |
apt remove --purge pkg | Yes | Yes | Single-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.
Remove multiple related packages
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.
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.
- Take a snapshot or backup
/etcand the database before major removals. - Put the app in maintenance mode or drain traffic if you remove the web stack.
- Stop the service tied to the package:
sudo systemctl stop nginx. - Run
apt remove --purgeon the target packages. - Run
apt autoremove --purgeonly after reviewing the candidate list. - Reload systemd:
sudo systemctl daemon-reload. - Verify ports, PHP-FPM pools, and cron paths still resolve.
- 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/binor your home directory. - Remove systemd unit files under
/etc/systemd/system/. - Search cron with
crontab -land/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.
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.
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 -landapt-cache rdependsbefore any production removal. - Use
apt remove --purgefor full uninstall including/etcconfigs on web servers. - Review the
autoremovecandidate list line by line — never accept it blindly on live boxes. - Match uninstall tools to install method: APT for deb packages,
snap removefor Snaps, manual cleanup for nvm and Composer globals. - Back up
/etc, runapt 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 updateclean.
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
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.

