Cleaning up an Ubuntu OS
30 Nov 2023 - João Porto
Some useful commands to clean up an Ubuntu OS of a coder.
Motivation
Recently I faced problems with my laptop storage. In some point, I couldn’t even create simple text files. So I decided to clean up the storage.
Exploring storage usage
I used the Ubuntu’s tool Disk Usage Analyzer to understand where there were extensive use of the storage.
Additionally, I used the following command to check a current folder size:
foo@bar:~$ du -hs
And the following to check the devices usage:
foo@bar:~$ df -h
Besides some obvious files that I already knew that were really large and I moved to my Drive, I saw that the following folders were huge (is not sorted by size):
/var/log/journal
~/.config/Code
~/.config/google-chrome
~/.../.git
Also, I checked the applications installed in the laptop, verifying whether make sense or not to keep them installed.
foo@bar:~$ apt list --installed
foo@bar:~$ snap list
Finally, I had a suspicion that Docker images also were contributing to the problem.
Cleaning up journal logs
Journal, as far as I remember, is a Linux’s feature that keeps the OS consistent in the case of sudden shutdowns.
I wondered whether was safe or not remove these logs, so I searched about that and apparently is safe. (3 months since the last clean up and no problem.)
foo@bar:~$ sudo journalctl --disk-usage
foo@bar:~$ sudo journalctl --rotate
foo@bar:~$ sudo journalctl --vacuum-time=2days
In short words, the first command print the journal log’s usage and the others are used to clean up. You can find a good explanation about these commands here.
The only thing that I can say is that they worked very well.
Cleaning up Docker images
There were things as unused and dangling images. They occupy space and are not useful for me.
You can remove dangling images using the following command:
foo@bar:~$ docker rmi $(docker images -a --filter=dangling=true -q)
This command
- Lists all the images
- Filters dangling images
- Extracts dangling images ids
- Removes the dangling images
And to remove stopped containers:
foo@bar:~$ docker rm $(docker ps --filter=status=exited --filter=status=created -q)
You can get more details about these commands here. Once again, the only thing that I can say is that they worked very well.
Cleaning up VS Code’s Workspaces
Maybe is not the reality of everybody, but in the past I used to clone projects to explore them (this changed when I discovered GitHub Codespaces).
Occurs that even deleting a project’s folder, VS Code keeps data of this project, and this is the why for a so huge ~/.config/Code
.
So I found a really useful extension called Workspace Storage Cleanup.
It finds those kind of broken workspaces and provides options to delete them.
After install the extension, from your VS Code:
- Ctrl + Shift + p
- Search for
Workspaces: Cleanup Storage
- Toggle folder missing
- Delete Selected
Cleaning up unnecessary applications
You can remove applications installed with apt
or snap
.
For example, I removed Discord that was installed with apt
:
foo@bar:~$ sudo apt remove --purge discord
Also, you may want to remove all configuration files of already removed apps:
foo@bar:~$ sudo apt purge ?config-files
And I removed Postman that was installed with snap
:
foo@bar:~$ snap remove postman
Chrome and Git
I didn’t find a good and safe way to clean up a ~/.../.git
folder.
Finally, Chrome data could be deleted from the GUI, but I thought was not worth it because a lot of cached data is relevant to me.
Conclusion
As I said early, in the beginning of the clean up process, I could not even create a file, now I have 23 GiB of free space. Is not too much, but is better now.
References
M, Ji. “Free up Disk Space – Clear Systemd Journal Logs in Ubuntu 20.04 | 22.04 | UbuntuHandbook.” Ubuntuhandbook.org, 31 July 2023, ubuntuhandbook.org/index.php/2020/12/clear-systemd-journal-logs-ubuntu. Accessed 30 Nov. 2023.
Neff, Andy. “How to Delete Cache?” Docker Community Forums, 21 Jan. 2016, forums.docker.com/t/how-to-delete-cache/5753. Accessed 30 Nov. 2023.
Morm, Serey. “What Is a Dangling Image and What Is an Unused Image?” Stack Overflow, 17 July 2017, stackoverflow.com/questions/45142528/what-is-a-dangling-image-and-what-is-an-unused-image. Accessed 30 Jan. 2023.
“How to Clear Git Cache | Learn Git Clear Cache in Different Ways – Junos Notes.” Junos Notes, 22 Oct. 2023, www.junosnotes.com/git/how-to-clear-git-cache/#:~:text=To%20clear%20your%20entire%20Git. Accessed 30 Nov. 2023.
git-scm.com. “Git - Git-Rm Documentation.” Git-Scm.com, git-scm.com/docs/git-rm#Documentation/git-rm.txt—cached. Accessed 30 Nov. 2023.
starball. “What Are the Effects of Clearing the WorkspaceStorage Folder for Visual Studio Code?” Stack Overflow, 7 Mar. 2023, stackoverflow.com/questions/61056128/what-are-the-effects-of-clearing-the-workspacestorage-folder-for-visual-studio-c. Accessed 30 Nov. 2023.
Charlie, Leger. “How to Use the Extension · Issue #6 · Mehyaa/Vscode-Workspace-Storage-Cleanup.” GitHub, 7 July 2023, github.com/mehyaa/vscode-workspace-storage-cleanup/issues/6. Accessed 30 Nov. 2023.
Wikipedia contributors. “Disk Usage Analyzer.” Wikipedia, 20 Aug. 2022, en.wikipedia.org/wiki/Disk_Usage_Analyzer. Accessed 29 Jan. 2024.
Linuxopsys. “How to List Installed Packages by Size on Ubuntu.” LinuxOPsys, 3 Sept. 2022, linuxopsys.com/topics/list-installed-packages-by-size-on-ubuntu#:~:text=You%20can%20also%20use%20the. Accessed 29 Jan. 2024.
Pilot, Ansible. “Managing Journal Logs in Fedora: Can I Remove Files in /Var/Log/Journal?” Medium, 20 Nov. 2023, ansiblepilot.medium.com/managing-journal-logs-in-fedora-can-i-remove-files-in-var-log-journal-83cc59e7a8cc#:~:text=Yes%2C%20you%20can%20safely%20delete. Accessed 29 Jan. 2024.
nobody. “What Exactly Does “Apt Purge ?Config-Files” Do?” Unix & Linux Stack Exchange, 12 Oct. 2023, unix.stackexchange.com/questions/758736/what-exactly-does-apt-purge-config-files-do#:~:text=When%20you%20do%20a%20apt. Accessed 29 Jan. 2024.