When an APT installation or upgrade process in Ubuntu is interrupted by an unexpected power cut, terminal crash, or broken pre-removal script, package management operations grind to an absolute halt. Any subsequent attempt to install, remove, or upgrade packages fails with a severe error: “dpkg: error processing package [packagename] (–remove): package is in a very bad inconsistent state; you should reinstall it before attempting a removal.”
This message signifies that the low-level Debian package manager (dpkg) has recorded the package as half-installed or half-configured inside the status database (/var/lib/dpkg/status). Because package integrity is compromised, dpkg refuses to execute standard commands until the inconsistency is cleared. This guide details how to force remove the damaged package and restore healthy package management.
Method 1: Force Removal Using dpkg –remove –force-remove-reinstreq
The standard apt-get remove command respects safety boundaries and will refuse to touch packages flagged for required reinstallation. The low-level dpkg utility provides a direct force flag designed specifically for this scenario.
- Press Ctrl + Alt + T to launch the terminal.
- Identify the exact package name reported in the error message (e.g. samba-common or libssl1.1).
- Force the removal of the inconsistent package by running:
Replacesudo dpkg --remove --force-remove-reinstreq [packagename][packagename]with the exact name of the broken package. - If the package is successfully removed, repair broken package dependencies:
sudo apt-get install -f - Update your package lists to verify normal operation:
sudo apt-get update && sudo apt-get upgrade
Method 2: Clear Corrupted Package Info Files
If forcing removal with --force-remove-reinstreq fails because pre-removal scripts (.prerm) or post-installation scripts (.postinst) throw execution errors, moving the package’s script files aside allows dpkg to bypass the failing hook.
- Open the terminal window.
- Navigate to the dpkg package information directory:
cd /var/lib/dpkg/info - List all script files associated with the broken package:
ls -la [packagename].* - Move these script files into a temporary backup location:
sudo mkdir -p /tmp/dpkg-backup sudo mv [packagename].* /tmp/dpkg-backup/ - Now re-run the removal command:
sudo dpkg --remove --force-remove-reinstreq [packagename] - Because the failing maintenance scripts were removed, dpkg will cleanly unregister the package files and delete its status flags.
Method 3: Manually Clean the dpkg Status Database Entry
If dpkg still complains about an inconsistent state, you can manually edit the status database entry to change the package status from broken to purged.
- Create an immediate backup copy of your status database before editing:
sudo cp /var/lib/dpkg/status /var/lib/dpkg/status.backup - Open the status file in the
nanotext editor:sudo nano /var/lib/dpkg/status - Press Ctrl + W, type
Package: [packagename], and press Enter to find the package definition block. - Look for the line that reads:
orStatus: install ok reinstreqStatus: deinstall ok config-files - Either delete the entire paragraph describing that specific package (from
Package: ...down to the empty newline before the next package), or change the Status line to:Status: purge ok not-installed - Press Ctrl + O followed by Enter to save the file, then press Ctrl + X to exit nano.
- Run the package fixer command:
sudo apt-get update && sudo apt-get install -f
Method 4: Reinstall the Clean Package from Upstream
If the broken package is an essential system library that cannot simply be deleted without removing critical desktop packages, force-downloading a fresh .deb package and reinstalling it resolves the state cleanly.
- Download the official Debian archive for the package without installing it:
sudo apt-get download [packagename] - Force the reinstallation of the downloaded
.debarchive over the corrupt installation:sudo dpkg -i --force-all [packagename]*.deb - Configure unconfigured packages:
sudo dpkg --configure -a - Clean up temporary downloaded files:
rm [packagename]*.deb
dpkg Inconsistent State Recovery Reference Table
The table below provides a quick reference for resolving corrupted package states:
| Stage / Failure Point | Cause | Command |
|---|---|---|
| APT rejects remove command | reinstreq flag set in dpkg status | sudo dpkg --remove --force-remove-reinstreq <pkg> |
| prerm / postinst script failure | Broken maintainer bash script | sudo mv /var/lib/dpkg/info/<pkg>.* /tmp/ |
| Package database locked or corrupt | Orphaned dpkg lock or status corruption | Edit /var/lib/dpkg/status (after backup) |
| Essential system library broken | Missing binary or broken symlink | sudo apt-get download <pkg> && sudo dpkg -i --force-all *.deb |
Preventing dpkg Status Database Corruption
To avoid package state corruption, never close your terminal emulator or power off your computer while an APT transaction is actively writing files to disk. If an update appears to freeze, wait at least several minutes to verify whether the disk is performing post-installation triggers before attempting any manual intervention.