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

How to skip errors in mysql master-slave replication

1. Traditional binlog master-slave replication, s...

Detailed explanation of how to configure Nginx web server sample code

Overview Today we will mainly share how to config...

Summary of the benefits of deploying MySQL delayed slaves

Preface The master-slave replication relationship...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

Implementation example of uploading multiple attachments in Vue

Table of contents Preface Core code File shows pa...

js development plug-in to achieve tab effect

This article example shares the specific code of ...

How to monitor and delete timed out sessions in Tomcat

Preface I accidentally discovered that the half-h...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

SQL implementation of LeetCode (197. Rising temperature)

[LeetCode] 197.Rising Temperature Given a Weather...

W3C Tutorial (16): Other W3C Activities

This section provides an overview of some other i...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

How to start multiple MySQL instances in CentOS 7.0 (mysql-5.7.21)

Configuration Instructions Linux system: CentOS-7...