How to deal with the problem that the file is deleted but the space is not released in Linux

How to deal with the problem that the file is deleted but the space is not released in Linux

Background of the problem

The server monitoring system of a business system sent an early warning notification that the disk space usage rate had reached 90%. Then I logged into the server and searched for larger log files and deleted them all (the pit was buried here). Some disk space was freed up, but I was negligent at the time and did not confirm whether the space for the files that were found and deleted had been completely freed up. A few days later, the server was warned again. I was puzzled as to why the log grew so fast. After investigation, I found that it was because a large file space was not released after the last file deletion operation.

Problem restoration and solution

Find the data files that take up a lot of space

#Check disk space usage $ df -h
#First query the files that occupy a large space in the /tmp directory $ du -sh /tmp/*|sort -nr|head -3
#Query the files that occupy a large amount of space in the /home directory $ du -sh /home/*|sort -nr|head -3
# After finding the file, delete it. After deleting it, use df -h to check whether it has been released.

Explain why when looking for files, the files in the /tmp/* directory are searched first.

Linux system deletion policy: Linux does not have a recycle bin function, so the service will move the files to be deleted to the system /tmp directory first, and then regularly clear the data in the /tmp directory.

Many servers do not have a separate partition for /tmp when installing the system, so it is possible that the data in the /tmp directory occupies a large part of the space. You can first clear the files in the /tmp directory to free up space.

The problem of deleting files without releasing space occurred when deleting a dubbo service log file in the /home directory space.

Deleting files does not release space

Generally, the situation where space is not released after deleting a file will not occur. However, it may still occur when the file is locked by a process or a process keeps writing data to the file. If you understand the storage mechanism and storage structure of files under Linux, you will understand this problem.

Files in the Linux system are divided into two parts: the pointer part and the data part.

  • Pointer part: It exists in the meta-data of the file system. After we execute the rm command to delete the data, the pointer is cleared from the meta-data.
  • Data part: The data is stored directly on the disk. When the pointer is cleared from meta-data, the space occupied by the data part can be overwritten and new content can be written.

The reason why the space is not released after deleting the dubbo log file is that the dubbo process is still writing data to this file. When the file is deleted, the pointer is not cleared from the meta-data, so the log file still occupies space.

How to find such files

You can use the lsof command to get a list of files that have been deleted but are still occupied by the program:

lsof | grep delete

How to free up this space

There are many ways to solve this type of problem and free up space: restart the occupied process, restart the operating system, and use commands. The first two methods are most convenient for non-production environments, but for production environments, try to use commands, which are actually very simple:

echo " " >/home/dubbo/log/xxx.log

In this way, the occupied disk space will be released without affecting the continued execution of the process.

This concludes the article on how to handle the problem of deleted files but unreleased space in Linux. For more information on Linux file deletion without unreleased space, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the problem that the space is not released after the Linux file is deleted
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]
  • Linux unlink function and how to delete files
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to delete folders, files, and decompress commands on Linux servers
  • 5 Ways to Clear or Delete Large File Contents in Linux
  • Comparison of the efficiency of different methods of deleting files in Linux

<<:  Detailed explanation of MySQL/Java server support for emoji and problem solving

>>:  Solution to incomplete text display in el-tree

Recommend

Detailed steps for using AES.js in Vue

Use of AES encryption Data transmission encryptio...

Index in MySQL

Preface Let's get straight to the point. The ...

Detailed explanation of the use of redux in native WeChat applet development

premise In complex scenarios, a lot of data needs...

How does Vue implement communication between components?

Table of contents 1. Communication between father...

Steps for Docker to build its own local image repository

1. Environment and preparation 1. Ubuntu 14.04 2....

When is it appropriate to use dl, dt, and dd?

dl:Definition list Definition List dt:Definition t...

Solution to the problem that order by is not effective in MySQL subquery

By chance, I discovered that a SQL statement prod...

Learn how to use the supervisor watchdog in 3 minutes

Software and hardware environment centos7.6.1810 ...

jQuery implements accordion effects

This article shares the specific code of jQuery t...

Example of using mycat to implement MySQL database read-write separation

What is MyCAT A completely open source large data...

How to write the Nofollow tag and how to use it

The "nofollow" tag was proposed by Goog...