Debug School

rakesh kumar
rakesh kumar

Posted on

How to Safely Clean Up Server Disk Space Without Deleting Important Data

Inspect Apache logs
Find Laravel session directories
Inspect MySQL logs
Locate backups and installer archives
Check recovered space

df -kh
Enter fullscreen mode Exit fullscreen mode

output


/dev/sda1  387G  365G  22G  95%  /
Enter fullscreen mode Exit fullscreen mode
That means the root filesystem / is 95% full.
Enter fullscreen mode Exit fullscreen mode

Commands such as df show usage by filesystem, while du helps identify which directories and files consume that space. About df, about du

Where is /opt/lampp/htdocs stored?

Normally:

/opt/lampp/htdocs
Enter fullscreen mode Exit fullscreen mode

is nested like this:

/
└── opt
    └── lampp
        └── htdocs
            ├── project-1
            ├── project-2
            └── project-3
Enter fullscreen mode Exit fullscreen mode

Because /opt does not appear as a separate mounted filesystem in your df -kh output, it is stored inside:

/dev/sda1 mounted on /
Enter fullscreen mode Exit fullscreen mode

Yes, /dev/sda1 is your main filesystem:

Size: 387 GB
Used: 365 GB
Available: 22 GB
Usage: 95%
Mounted at: /
Enter fullscreen mode Exit fullscreen mode

Because it is mounted on /, begin searching from /.

  1. Find the largest main folders

This command is safe and does not delete anything:

sudo du -xhd1 / 2>/dev/null | sort -hr
Enter fullscreen mode Exit fullscreen mode

FASTER SEARCH COMMAND

sudo du -sh /opt /var /home /usr /root 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

output

next step

sudo du -xhd1 /opt 2>/dev/null | sort -hr
Enter fullscreen mode Exit fullscreen mode

output


next step

sudo du -xhd1 /opt/lampp 2>/dev/null | sort -hr
Enter fullscreen mode Exit fullscreen mode

output

next step


sudo du -xhd1 /opt/lampp/htdocs 2>/dev/null |
sort -hr |
head -30
Enter fullscreen mode Exit fullscreen mode

output

next step


sudo du -xhd1 /opt/lampp/htdocs/holidaylandmark 2>/dev/null |
sort -hr |
head -30
Enter fullscreen mode Exit fullscreen mode

output

You may get output like:

365G    /
220G    /opt
90G     /var
40G     /home
8G      /usr
Enter fullscreen mode Exit fullscreen mode

The largest folder is where you search next.

  1. Automatically show the 30 largest folders
sudo du -xhd2 / 2>/dev/null | sort -hr | head -30
Enter fullscreen mode Exit fullscreen mode

This checks two directory levels, for example:

220G    /opt/lampp
180G    /opt/lampp/htdocs
90G     /var/lib
Enter fullscreen mode Exit fullscreen mode

For deeper inspection:

sudo du -xhd3 / 2>/dev/null | sort -hr | head -50
Enter fullscreen mode Exit fullscreen mode

This may take some time but is read-only.

  1. Check your important server locations separately
sudo du -xhd1 /opt 2>/dev/null | sort -hr
sudo du -xhd1 /opt/lampp 2>/dev/null | sort -hr
sudo du -xhd1 /opt/lampp/htdocs 2>/dev/null | sort -hr | head -30
sudo du -xhd1 /var 2>/dev/null | sort -hr | head -30
sudo du -xhd1 /home 2>/dev/null | sort -hr | head -30
Enter fullscreen mode Exit fullscreen mode
  1. Find the 50 largest individual files

This command searches only /dev/sda1 and does not cross into tmpfs, /boot or other mounted filesystems:


sudo find / -xdev -type f -printf '%s\t%p\n' 2>/dev/null |
sort -nr |
head -50 |
numfmt --field=1 --to=iec
Enter fullscreen mode Exit fullscreen mode

Example output:

82G  /opt/lampp/logs/error_log
19G  /opt/lampp/htdocs/project/storage/framework/sessions/...
10G  /backup/old-backup.tar.gz
3G   /opt/lampp/var/mysql/mysql-slow.log
Enter fullscreen mode Exit fullscreen mode
  1. Find all files larger than 1 GB
sudo find / -xdev -type f -size +1G \
-printf '%s\t%p\n' 2>/dev/null |
sort -nr |
numfmt --field=1 --to=iec
Enter fullscreen mode Exit fullscreen mode
  1. Check if deleted files are still consuming space

If folder totals do not add up to approximately 365G, run:

sudo lsof +L1
Enter fullscreen mode Exit fullscreen mode

For only large deleted-but-open files:


sudo lsof +L1 2>/dev/null |
awk '$7 ~ /^[0-9]+$/ && $7 > 1073741824 {
    printf "%.2f GB\tPID=%s\tPROCESS=%s\t%s\n",
    $7/1073741824, $2, $1, $9
}'
Enter fullscreen mode Exit fullscreen mode

Such files continue using disk space until their owning service closes them or is safely restarted.

Best single command

To immediately see the largest folders and files under /dev/sda1:

sudo du -xah / 2>/dev/null | sort -hr | head -50
Enter fullscreen mode Exit fullscreen mode
  1. Inspect Apache logs
sudo du -ah /opt/lampp/logs 2>/dev/null | sort -h | tail -30
Enter fullscreen mode Exit fullscreen mode

If these are confirmed Apache log files, truncate them without deleting the files:

sudo truncate -s 0 /opt/lampp/logs/access_log
sudo truncate -s 0 /opt/lampp/logs/error_log
sudo truncate -s 0 /opt/lampp/logs/ssl_request_log 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Remove only already-rotated logs:

sudo find /opt/lampp/logs -maxdepth 1 -type f \
\( -name "*.gz" -o -name "*.[0-9]" -o -name "*.old" \) \
-print
Enter fullscreen mode Exit fullscreen mode

After checking the displayed files:

sudo find /opt/lampp/logs -maxdepth 1 -type f \
\( -name "*.gz" -o -name "*.[0-9]" -o -name "*.old" \) \
-delete
Enter fullscreen mode Exit fullscreen mode
  1. Find Laravel session directories

First list their sizes:

sudo find /home /opt/lampp/htdocs -type d \
-path "*/storage/framework/sessions" \
-exec du -sh {} \; 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Preview session files older than seven days:

sudo find /home /opt/lampp/htdocs -type f \
-path "*/storage/framework/sessions/*" \
-mtime +7 -print 2>/dev/null | head -100
Enter fullscreen mode Exit fullscreen mode

Delete only Laravel file-based sessions older than seven days:

sudo find /home /opt/lampp/htdocs -type f \
-path "*/storage/framework/sessions/*" \
-mtime +7 -delete 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

This logs out users whose sessions are older than seven days. If Laravel sessions are stored in a database, this command will not clean them.

  1. Inspect MySQL logs
sudo find /opt/lampp/var/mysql -maxdepth 1 -type f \
\( -name "*.err" -o -name "*slow*.log" -o -name "mysql-bin.*" \) \
-exec ls -lh {} \; 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Truncate only active error and slow-query logs:

sudo find /opt/lampp/var/mysql -maxdepth 1 -type f \
\( -name "*.err" -o -name "*slow*.log" \) \
-exec truncate -s 0 {} \;
Enter fullscreen mode Exit fullscreen mode

Do not manually delete mysql-bin.* files. Purge old binary logs through MySQL:

sudo /opt/lampp/bin/mysql -u root -p \
-e "PURGE BINARY LOGS BEFORE (NOW() - INTERVAL 7 DAY);"
Enter fullscreen mode Exit fullscreen mode
  1. Locate backups and installer archives

Preview likely backup and archive files:

sudo find /home /opt /var/backups -xdev -type f \
\( -name "*.sql" -o -name "*.sql.gz" -o -name "*.tar" \
-o -name "*.tar.gz" -o -name "*.zip" \) \
-size +100M -printf '%10s bytes  %TY-%Tm-%Td  %p\n' 2>/dev/null |
sort -nr
Enter fullscreen mode Exit fullscreen mode

Do not delete these automatically. Some may be your only valid backups. Delete files only after identifying the exact obsolete paths:

sudo rm -- "/exact/path/to/confirmed-old-backup.tar.gz"
Enter fullscreen mode Exit fullscreen mode
  1. Check recovered space
df -h /
sudo du -sh /opt/lampp/logs /opt/lampp/var/mysql 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

The Apache and MySQL log commands preserve active files. For the claimed 10 GB backups and 0.4 GB installers, first use the preview command and delete only explicitly confirmed files.

Top comments (0)