[How To] Undo a recent upgrade that broke things

It is well advised to run your updates and upgrades regularly.
However, on occasion something can still go wrong.
Often, you can narrow things down to the actual program upgrade that caused a new problem.
But in the event that you cannot and you just want to undo all that was done since that date, that is what this post is for.

Before doing anything - BackUp your Data!

First, use grep to gather the installs since a date that you ran the upgrades. Pipe that to a text file. Replace the generic 2023-01-01 01:01:01 date - time below with the date time you need:

grep -A 2 'Start-Date: 2023-01-01  01:01:01' /var/log/apt/history.log | tail -1 >/tmp/upgraded.txt

You may need to modify the above to something like: /var/log/apt/history.log.1.gz
Check your /var/log/apt directory first.

That piped output will need some cleaning up. It contains the word "install" that will need to be removed and it will need some further clarification in order to create a file we can run.

sed -i 's/Install://' /tmp/upgraded.txt

Let's continue creating our Undo Upgrade file. We don't want it to remove all your installed software, after-all...:

tr ',' '\n' < /tmp/upgraded.txt | sed '/automatic)/d' | awk '{ print $1}' > /tmp/undo-upgraded.txt
wc -l /tmp/undo-upgraded.txt

Now we have a file in /tmp named undo-upgraded.txt

Just like in this thread:

We want to run this as an apt call.

sudo u="$(</tmp/undo-upgraded.txt)"

Now we can call on that .txt file as our remove --purge list:

sudo apt remove --purge $u

Final cleaning:

sudo apt clean && sudo apt autoremove


Might be useful for rolling releases where updates can frequently be broken.