Linux Commands

How to Fix the “No space left on device” Error in Linux

If you are a Linux user, you may have encountered the “No space left on device” error message when trying to save, create, or install something on your system. This error message can be frustrating and confusing, especially if you are not familiar with the underlying cause of the issue.

In this article, we will explain what causes the “No space left on device” error in Linux and provide step-by-step instructions on how to fix it.

What Causes the “No Space Left on Device” Error in Linux?

The “No space left on device” error in Linux occurs when your system has run out of disk space. This error can happen for multiple reasons some of them are:

Now that we understand the cause of the “No space left on device” error, let’s dive into how to fix it.

How to Fix the “No space left on device” Error in Linux?

There are several reasons related to the storage that cause this error. Let’s discuss them one by one and their respective solutions.

Reason 1: Deleted Files Taking up Space

When we delete a file from a Linux system the space taken by the file is not automatically released by the system. As a result, the space keeps getting filled up with deleted files, causing the system to run out of space.

Solution: Free up the Occupied Storage

The best solution to this problem is to release the storage space occupied by deleted files. Now list all the deleted processes using the command below:

sudo lsof / | grep deleted

Once you have identified the deleted processes, you can release the storage space by performing a reload using the command below:

sudo systemctl daemon-reload

This command will ensure that the system releases the storage space for any deleted files.

Reason 2: Temporary Files Taking Space

A large number of temporary files accumulate over time, consuming disk space. To delete these temporary files, follow the given solution.

Solution: Clean Up Temporary Files

Open your terminal run:

sudo du -sh /tmp/*

This command will give us the size of files that are stored in a temporary directory.

Run the below mentioned command to delete all files stored inside the temporary directory.

sudo rm -rf /tmp/*

To confirm all the files got deleted again run the command:

sudo du -sh /tmp/*

Reason 3: Corrupted Blocks

Over time, unused storage blocks or other reasons can cause blocks to become corrupted, which the operating system will no longer recognize. This can result in the “No space left on device” error.

Solution: Repair the Corrupted Blocks

To resolve this issue, you need to mark the corrupted blocks as unusable so that the operating system can recognize them and repair them. Below command will give us list of the blocks:

lsblk -a

To mark the corrupted blocks as unusable so system can recognize them, use the fsck command to check and repair the Linux filesystems:

sudo fsck -vcck [DEVICE_NAME]

Run given below command to check the device name:

sudo fdisk -l

Following result about system devices will appear showing us the size used by devices:

Alternatively, you can also use following command to check the current size of different partition:

sudo df -h

This command will show you the disk usage of all the partitions in your system. Identify which partition has low disk space.

Type the following command and replace “/dev/sda2” with the partition name you want to check, and press Enter:

sudo fsck -vcck /dev/sda2

This command will check and repair the file system.

Note: By marking the corrupted blocks, your data will be reallocated from those blocks, and the system will perform a check on these sectors. This will replace all the corrupted blocks with functioning ones. The fsck command cannot be executed on the same filesystem being tested, so use a live CD to perform this task.

Reason 4: Limited Inode Count

Inodes are data structures used by the file system to store information about files and directories. Whenever a system runs out of disk space it also runs out of inodes.

Solution: Increase Inode Count

Find the current inodes count using command:

sudo df -i /

It is important to ensure that the column for available Inodes (Ifree) is not at zero. When all Inodes are used up, it is necessary to delete some old files from the base directory associated with the file location (Filesystem) to free up space.

Conclusion

The “No space left on device” error can occur in Linux because of corrupted blocks or deleted files that are still stored in the system. The first solution involves performing a daemon-reload, which releases the storage space that is being occupied by deleted files. The second solution is to use the fsck command to mark corrupted storage blocks as unusable, allowing the Linux OS to recognize them and repair them. By using these methods, you can resolve this error and avoid any issues related to storage space in the future.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.