What files can be deleted if there is not enough disk space on Linux

There are situations when the disk space has run out completely and you need to urgently clean the disk and delete files. Disk space may run out even so that

  • when you try to clear the installation package cache, the system will report an error (there is no place even to save the lock files),
  • when you try to install ncdu to search for overgrown folders and files, the system will not be able to find even 81 kilobytes needed for this command
  • when trying to find files and folders that occupy the most space in Linux, the system will also give an error due to the fact that the sort command cannot save the data cache to disk if there is a lot of this data

That is, there are really critical situations – in these conditions, many programs and services cease to work normally. Therefore, the task becomes the following: immediately free up disk space at any cost so that you can continue to maintain the system and go to the second stage – search for directories and folders that led to this problem due to the fact that they began to take up too much space.

I will warn you in advance: the following commands, although basically delete useless files, after their execution you may need actions such as:

  • restarting services for their normal operation (so that they recreate log files, caches, lock files)
  • various logs and files from the recycle bin may be lost, which although most users do not need them, in some conditions you may want to keep them (for example, it is important for you to investigate the log files, as they may contain the cause of the problem).

This means that DO NOT mindlessly copy-paste commands – read he explanations to them and evaluate how painless they are for your situation.

1. Delete temporary files

The files in the /tmp/ folder will be deleted anyway the next time the system is rebooted. That is, on the one hand, they can be removed quite painlessly:

sudo rm -rf /tmp/*

BUT: the work of programs that are currently running and which have saved some data in the /tmp/ folder may be disrupted.

2. Removing cache files

There are many subdirectories in the /var/cache/ directory that can be deleted almost painlessly (data will not be lost, and programs will create new cache files). This directory is of particular interest because on some systems caches grow into gigabytes and tens of gigabytes. Sometimes, finding the problem directory in /var/cache/ can finally solve the situation with a lack of disk space.

To delete the font cache:

sudo rm -rf /var/cache/fontconfig/

To remove the installation package cache (on Debian, Linux Mint, Ubuntu, Kali Linux and their derivatives):

sudo rm -rf /var/cache/apt/

To remove the installation package cache (on Arch Linux, BlackArch and their derivatives):

sudo rm -rf /var/cache/pacman/

Removing the manual page cache:

sudo rm -rf /var/cache/man/

You can continue to search for large caches applicable to the software installed on your system. For example, it could be the caches of a web server, proxy server, etc.

3. Deleting logs

You can delete almost all files in this (/var/log/) folder, but try to keep the folder structure, since some applications, after deleting the folder here, are not able to recreate it…

Web server logs can grow too much on web servers.

To delete Apache logs on Debian, Linux Mint, Ubuntu, Kali Linux and their derivatives:

sudo rm -rf /var/log/apache2/*

To delete Apache logs on Arch Linux, BlackArch and their derivatives:

sudo rm -rf /var/log/httpd/*

For the server to start creating new log files and writing to them, you need to restart the web server service.

Depending on the intensity of use of the system, accumulated logs can occupy gigabytes. Depending on the system, files can be named in different ways; more accurate analysis is recommended using the ncdu utility:

sudo ncdu /var/log/

4. Empty the trash bin

This tip is more for desktop systems. Files that you deleted in the graphical interface of the desktop fall into the ~/.local/share/Trash/files/ folder, you can analyze them and delete them (second time) if you wish:

ncdu ~/.local/share/Trash/files/

5. Removing unnecessary kernel header source files

The following applies only to Debian, Linux Mint, Ubuntu, Kali Linux and their derivatives. Check the folder /usr/src/, there will be subfolders of the form linux-headers-, most of them can be deleted – leave only the one whose number corresponds to the current kernel of the system – usually this is the latest release number.

6. Removing orphaned packages

Orphaned packages are those packages (programs) that were installed as dependencies for other programs. But for various reasons, they are no longer needed: either the program that used them was removed, or for that program they ceased to be dependencies after updating the program.

On Debian, Linux Mint, Ubuntu, Kali Linux and their derivatives, you can remove unnecessary packages as follows:

sudo apt autoremove

For Debian and its derivatives, the previous command is completely safe.

In Arch Linux and derivatives, the list of orphaned packages can be seen as follows:

pacman -Qdt

Before proceeding to their automatic removal, it is strongly recommended that you pay attention to this list!

To recursively remove orphans and their configuration files in Arch Linux and derivatives:

sudo pacman -Rns $(pacman -Qtdq)

If no orphaned packages were found, pacman will exit with an error: no targets specified (use -h for help). This is expected since pacman -Rns received no arguments.

7. Files in the /lost+found directory

Files that were found after checking the disk file system are saved in the /lost+found folder. Typically, such checks are performed after a sudden reboot of the system or in case of signs of disk problems.

Found files are usually damaged. Their goal is to save data that, if errors were fixed on the file system, would be completely lost.

The /lost+found folder may be empty (if there were no problems with the disk). If there are files there, then you can review them and, if desired, delete them.

8. Cleaning up PHP sessions

Sometimes web applications can create countless sessions due to a bug. Check the directory /var/lib/php/sessions/ for too many files.

(BONUS) 9. Analyze Docker Files

Do not madly delete Docker files. I give an example of this directory only for the reason that it attracted my attention because of its simply phantasmagorical size – and this despite the fact that I do not actually use Docker – I tried several times to see what it was.

The largest folder is /var/lib/docker/overlay2/. To analyze the consumed disk space:

sudo ncdu /var/lib/docker/

Conclusion

So, we examined what files can be deleted from Linux with virtually no loss of information. In addition to the above directories, which can be cleaned both on a remote system with a web server and on a home computer, users with a graphical desktop should pay attention to such directories as:

  • ~/.cache
  • ~/.local
  • ~/Downloads (your downloaded files)

They DO NOT need to be removed, but worth analyzing. For example, you can find out that the web browser on your computer has a cache of several gigabytes and that you can free them (it is recommended to do it using the web browser, rather than deleting files directly).

If I missed some directories with files that can be deleted painlessly, then write them in the comments!

Last Updated on

Recommended for you:

Leave a Reply

Your email address will not be published.