A bash script to completely remove stuck snaps from Ubuntu/Linux systems when normal snap remove fails.
Sometimes snap remove fails and leaves snaps in a broken state with stuck changes in snapd's internal database. Common errors include:
error: snap "package-name" has "remove-snap" change in progress
Even after deleting directories and restarting snapd, the snap remains in a broken state and cannot be removed.
This script performs a complete nuclear removal by:
- ✅ Removing the snap from snapd's internal database (
.data.snaps) - ✅ Marking all related changes as Done (status 0)
- ✅ Marking all related tasks as Completed (status 4)
- ✅ Setting ready-time timestamps on all changes
- ✅ Cleaning up remaining directories (
~/snap,/var/snap,/snap) - ✅ Verifying complete removal
- Ubuntu/Linux with snapd installed
sudoaccessjqinstalled (sudo apt install jq)
sudo ./complete-snap-removal.sh <snap-name>sudo ./complete-snap-removal.sh skypesudo ./complete-snap-removal.sh slackThe script:
- Checks current status - Shows the snap and all related changes
- Interactive confirmation - Asks for confirmation before proceeding
- Creates timestamped backup - Backs up
/var/lib/snapd/state.json - Stops snapd - Safely stops snapd services
- Modifies state.json - Updates snapd's internal database using jq
- Restarts snapd - With automatic rollback if restart fails
- Cleans directories - Removes leftover snap directories
- Verifies removal - Confirms snap is completely gone
- 🔒 Automatic backups - Creates timestamped backup before any changes
- 🔒 JSON validation - Validates state.json before applying changes
- 🔒 Automatic rollback - Restores backup if snapd fails to restart
- 🔒 Interactive confirmation - Shows what will be removed before proceeding
- 🔒 Non-destructive for other snaps - Only affects the specified snap
If something goes wrong, the script shows you how to restore:
sudo systemctl stop snapd.service snapd.socket
sudo cp /var/lib/snapd/state.json.backup-remove-<snap>-<timestamp> /var/lib/snapd/state.json
sudo systemctl start snapd.service snapd.socketBackups are located at: /var/lib/snapd/state.json.backup-remove-<snap-name>-<timestamp>
The script modifies /var/lib/snapd/state.json:
- Removes snap entry from
.data.snapsobject - Sets change status to
0(Done) for all related changes - Sets task status to
4(Completed) for all related tasks - Sets ready-time to current timestamp for all related changes
These are the same values that snapd uses for successfully completed operations.
- Ubuntu 22.04 LTS
- Ubuntu 24.04 LTS
MIT License - Feel free to use, modify, and distribute.
Found a bug or have a suggestion? Feel free to open an issue or submit a pull request.
Created to solve persistent snap removal issues when normal methods fail.