Sobes.tech
Junior — Middle

What methods can be used to clean disk space without restarting the running service?

sobes.tech AI

Answer from AI

To free up disk space without restarting a running service, you can use the following methods:

  • Deleting unnecessary files and logs: Clearing temporary files, old logs, cache.
  • Freeing log space with log managers: For example, using logrotate with the copytruncate option, which allows truncating log files without restarting the service.
  • Deleting deleted but still open files: Sometimes files are deleted from the filesystem, but the process still holds them open, occupying space. You can find such files using lsof and free space by restarting or sending a signal to the process (e.g., kill -HUP) to close the descriptors.
  • Clearing application or database caches: Many services have commands or APIs to clear internal caches without stopping.

Example of using logrotate with copytruncate:

# In logrotate configuration
/var/log/myapp/*.log {
    daily
    copytruncate
    rotate 7
    compress
    missingok
    notifempty
}

This allows truncating logs without interrupting the service.