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

In Linux, there may be a few errors whose reasons are unclear to specify what causes them or what is the solution. The “No space left on device” error is one of these vague errors that leaves the user unsure of the actual reason. If the error is invoked, the storage of the system will no longer be usable.

This post aims to list the possible reasons that invoke the error “No space left on device” and the solutions to all those reasons are also demonstrated.

  1. What is the “No space left on device” Error in Linux?
  2. What are the Possible Reasons for the “No space left on device” Error in Linux?  
  3. How to Fix/Resolve the “No space left on device” Error in Linux?

What is the “No space left on device” Error in Linux?

In Linux, this error is a common problem that occurs when the file system runs out of free space. This can happen due to several reasons, such as creating too many files, deleting files without freeing up the disk space or having a corrupted file system. 

When the “No space left on device” error occurs, the user cannot write any new data to the file system, and some applications may fail to run properly.

What are the Possible Reasons for the “No space left on device” Error in Linux?  

This error indicates that the file system has run out of disk space. This can happen for various reasons, such as:

  • The disk is physically full and cannot store any more data.
  • The disk has reached its quota limit, which is a mechanism to restrict the disk space used by a group or a user.
  • The disk has too many small files, which consume more inodes than the actual data size. Inodes are data structures that store details about files as well as directories.
  • The disk has a corrupted file system, which prevents it from allocating new blocks or inodes.

This can cause various problems, such as crashing applications, corrupted files, or slow performance. 

Let’s fix the “No space left on device” error to free up some space on your disk or partition.

How to Fix/Resolve the “No space left on device” Issue?

This error means that your disk or partition is full and you cannot write any more data to it. There are a few reasons, referring to the storage, which can be causing this error depending on the situation and your preferences.

This section tackles these reasons and provides possible solutions for “No space left on device” issue.

Reason 1: Deleted Files Taking Space

Whenever a process is still in use and the file is deleted from the system, the disk space utilized by that file is not emptied. This is because the file is still being used by a process that has not been terminated.

Instead, the space is kept by the system and not released automatically. Due to this, the space keeps getting filled up with files that have already been deleted by the system. 

Solution 1: Release the Occupied Storage

The best way to fix this issue is to free up the storage that the system is occupying for deleted files. To accomplish this, first, you need to see the list of all the deleted processes using the “lsof” and “grep” commands. Then, restart the affected process and remove the process using this command:

Step 1: List of All the Deleted Processes 

To see all processes that contain spaces and cause an error “No space left on device”, use the “lsof” and “grep” commands with the “deleted” keyword:

sudo lsof / | grep deleted

It displays all those processes whose files are deleted but containing spaces in the Linux operating system.

Step 2: Restart the Affected Process

Now, users can restart that process which consumes a lot of space. For this, use the “systemctl” command with the “restart” by specifying the process name. In our case, restart the “cron” process:

sudo systemctl restart cron

In this way, the cron services are restarted or reloaded in the system.

Note: Users can also mention the affected service name to fix the “No space left on device” error in Linux.

Step 3: Reload Processes

Now, release the storage by performing a reload system via the “daemon-reload” option. It renews all dependent files and does not any modifications in the filesystem:

sudo systemctl daemon-reload

This will make sure that storage relating to any deleted files is released by the system.

Solution 2: Find and Kill Processes

Sometimes, when users delete a large file in Linux, see the error message “No space left on device” when trying to create a new file. To fix the above-mentioned problem, find and kill the processes that are using the deleted file. Here are the steps to do that:

Step 1: List All Processes 

To list all the processes that are using deleted files, use the “lsof” command with the L1 option. In this way, users can see the process ID (PID), the file name, as well as the size of the deleted file:

lsof +L1

It displays all the processes with the disk space as well as the process ID and sizes.

Step 2: Sort and Identify Processes 

Now, users can identify the processes that are using a large amount of disk space. For this, sort the output by size using the “sort” option: 

lsof +L1 | sort -n -k7

It enlists the processes based on ascending order:

Step 3: Kill/Terminate Specific Process

Alternatively, users can use the command “kill -9 PID” to force the process to terminate, but this may cause data loss or corruption:

kill -9 3638

It terminates the specific process forcefully.

Bonus: Restart Specific Process

Restart the processes that are using the deleted files. For this, use the “kill -HUP PID” command to send a hangup signal to the process. It reloads the configuration and releases the deleted file:

kill -HUP 1482

In this way, it reloads or restarts the configuration of the specific process.

Solution 3: Delete Unnecessary or Temporary Files 

Another solution is to use the “rm” command to remove files as well as directories. Also, use the “find” command with the “delete” option to delete files that match certain criteria. For example, to delete all files in the “/tmp” directory that are older than 10 days, use the below command:

sudo find /tmp -type f -mtime +10 -delete

In this way, all files that are older than 10 have been deleted from this directory.

Alternatively, users can also delete the “tmp” directory including all its content using the “rm” command as below:

sudo rm -rf /tmp/*

It deletes all the relevant content of the “tmp” folder/directory.

Reason 2: Corrupted Blocks

Over time if storage blocks are not utilized or for other reasons. They may become corrupted and the OS may stop recognizing them. This will cause the system to prompt the “no space left on device” error.

Solution: Repair the Blocks

To fix this issue, the first step is to mark these blocks as corrupted for the system so that it can recognize them and then start performing repairs on these blocks. To get a list of the blocks, run the command below:

lsblk -a

In this way, users can analyze the free as well as occupied space.

Start Repairing Blocks

To mark the specified block, start the repairing process using the “e2fsck” command with the superblock “8193”. In our case, the “/dev/sda2” is selected in the terminal:

e2fsck -b 8193 /dev/sda2

Marking the bad blocks will reallocate your data from those blocks and force you to check these sectors. This checking will replace all the bad blocks with working ones. You can replace “/dev/sda2” with any other device name that you might have on your system.

Another important tip is to make sure that you perform this task using a live CD since it is not possible to perform the “fsck” task on the same filesystem as the one that is being tested. 

Check Available Disk 

Verify that the disk space has been freed up. You can use the command “df” command with the “h” option to check the available disk space on the system:

df -h

In this way, users can analyze the space that has been available from the device.

Conclusion 

The “No space left on device” error occurs due to corrupted blocks on the disk or due to deleted files taking up space. This can be resolved by performing a daemon reload on your system. It will free up all the occupied space for deleted files or you can use the “e2fsck” command to mark corrupted storage blocks. Here, you have learned the reasoning behind this error and the methods that can be used to resolve it.