The dpkg post-removal script error occurs when a package's cleanup script fails during uninstallation or upgrade. This typically affects kernel images, system libraries, or packages with custom removal logic. Fix it by reconfiguring dpkg, repairing broken dependencies, or manually editing the problematic script.
When you remove or upgrade a package on Debian/Ubuntu systems, dpkg executes a "post-removal script" (postrm) to clean up package-specific configurations, directories, or system state. If this script returns exit status 1 (or any non-zero value), dpkg treats it as a failure and reports "subprocess installed post-removal script returned error exit status 1". The error indicates that the cleanup process didn't complete successfully. This can leave the system in an inconsistent state where the package is partially removed or the dpkg database is locked, preventing further package operations until resolved.
This is the first and safest fix. It tells dpkg to process and configure all partially installed packages:
sudo dpkg --configure -aThe -a flag means "all" and --configure tells dpkg to complete configuration of unpacked packages. This often resolves the error if the package was interrupted during installation.
If the above doesn't work, use apt to fix broken dependencies:
sudo apt install -fOr equivalently:
sudo apt install --fix-brokenThis command attempts to repair broken package dependencies and may complete the installation of the problematic package.
Clear the apt cache and try the operation again:
sudo apt clean
sudo apt update
sudo apt upgradeThis removes potentially corrupted package files and refreshes the package index.
If the post-removal script has DOS line endings (CR-LF), it will fail. First, identify the problematic package name from the error message, then fix it:
# If you have dos2unix installed:
sudo dos2unix /var/lib/dpkg/info/<package-name>.postrm
# Or replace the script with a minimal placeholder:
sudo sh -c 'echo "#!/bin/sh" > /var/lib/dpkg/info/<package-name>.postrm'After fixing the script, retry the operation:
sudo apt remove <package-name>Replace <package-name> with the actual package name from the error message (without the .postrm extension).
If the script contains a failing command, you can disable the -e flag (exit on error):
# First, back up the dpkg database
sudo cp -r /var/lib/dpkg /var/lib/dpkg.backup
# Edit the postrm script to comment out the set -e line
sudo sed -i 's/^set -e/#set -e/g' /var/lib/dpkg/info/<package-name>.postrmThis makes the script continue even if individual commands fail. Retry the operation after this change.
If standard removal fails, try forcing it with --force options:
# Attempt forced removal
sudo apt remove --allow-downgrades --allow-remove-essential --allow-change-held-packages <package-name>
# Or use dpkg directly with --force-all
sudo dpkg --remove --force-all <package-name>
# Then clean up
sudo apt install -f
sudo dpkg --configure -aUse this only after trying other methods, as it bypasses safety checks.
For kernel-related post-removal errors, the issue often involves /etc/kernel/postrm.d/ scripts failing, particularly update-grub or initramfs-tools. These are triggered when removing linux-image packages. If grub-customizer was used to modify GRUB configuration, it can break these scripts. In such cases, reinstalling grub-common or using the standard grub tools instead of grub-customizer may be necessary.
For DKMS (Dynamic Kernel Module Support) packages, the post-removal script may fail if the module failed to compile or uninstall. Run sudo apt remove <dkms-package> and then reinstall it cleanly.
If none of these methods work, as an absolute last resort, you can manually edit /var/lib/dpkg/status and /var/lib/dpkg/status-old to remove the problematic package entry entirely, but only do this with a backup of these files first. This is dangerous and should only be considered if the system is unusable and other methods have failed.
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
dpkg: error processing package package-name (--install)
How to fix "dpkg: error processing package" in apt