Linux headers are consuming a lot of disk space on the EC2 machine. Is it safe to manually delete the headers with rm command?

Posted on Jun 11, 2022

Question

EC2 machine on aws is filled with multiple linux headers and currently / is 100% full on disk space. I ran commands like apt-get autoremove but it is throwing an error saying

No apport report written because the error message indicates a disk full error
dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)

Can I remove the old headers by the rm command? Is it safe? and if I remove with rm command, should I do any clean up after that? Here is the list of headers:

du -h -d1 /usr/src/ | sort -hr
2.8G    /usr/src/
106M    /usr/src/linux-aws-headers-4.4.0-1099
106M    /usr/src/linux-aws-headers-4.4.0-1098
106M    /usr/src/linux-aws-headers-4.4.0-1096
106M    /usr/src/linux-aws-headers-4.4.0-1095
106M    /usr/src/linux-aws-headers-4.4.0-1094
106M    /usr/src/linux-aws-headers-4.4.0-1092
106M    /usr/src/linux-aws-headers-4.4.0-1090
106M    /usr/src/linux-aws-headers-4.4.0-1088
106M    /usr/src/linux-aws-headers-4.4.0-1087
106M    /usr/src/linux-aws-headers-4.4.0-1085
106M    /usr/src/linux-aws-headers-4.4.0-1084
106M    /usr/src/linux-aws-headers-4.4.0-1083
106M    /usr/src/linux-aws-headers-4.4.0-1079
106M    /usr/src/linux-aws-headers-4.4.0-1077
106M    /usr/src/linux-aws-headers-4.4.0-1075
106M    /usr/src/linux-aws-headers-4.4.0-1074
106M    /usr/src/linux-aws-headers-4.4.0-1072
106M    /usr/src/linux-aws-headers-4.4.0-1070
106M    /usr/src/linux-aws-headers-4.4.0-1069
106M    /usr/src/linux-aws-headers-4.4.0-1066
106M    /usr/src/linux-aws-headers-4.4.0-1065
106M    /usr/src/linux-aws-headers-4.4.0-1062
106M    /usr/src/linux-aws-headers-4.4.0-1061
106M    /usr/src/linux-aws-headers-4.4.0-1060
13M /usr/src/linux-headers-4.4.0-1100-aws
13M /usr/src/linux-headers-4.4.0-1099-aws
13M /usr/src/linux-headers-4.4.0-1098-aws
13M /usr/src/linux-headers-4.4.0-1096-aws
13M /usr/src/linux-headers-4.4.0-1095-aws
13M /usr/src/linux-headers-4.4.0-1094-aws
13M /usr/src/linux-headers-4.4.0-1092-aws
13M /usr/src/linux-headers-4.4.0-1090-aws
13M /usr/src/linux-headers-4.4.0-1088-aws
13M /usr/src/linux-headers-4.4.0-1087-aws
13M /usr/src/linux-headers-4.4.0-1085-aws
13M /usr/src/linux-headers-4.4.0-1084-aws
13M /usr/src/linux-headers-4.4.0-1083-aws
13M /usr/src/linux-headers-4.4.0-1079-aws
13M /usr/src/linux-headers-4.4.0-1077-aws
13M /usr/src/linux-headers-4.4.0-1075-aws
13M /usr/src/linux-headers-4.4.0-1074-aws
13M /usr/src/linux-headers-4.4.0-1072-aws
13M /usr/src/linux-headers-4.4.0-1070-aws
13M /usr/src/linux-headers-4.4.0-1069-aws
13M /usr/src/linux-headers-4.4.0-1066-aws
13M /usr/src/linux-headers-4.4.0-1065-aws
13M /usr/src/linux-headers-4.4.0-1062-aws
13M /usr/src/linux-headers-4.4.0-1061-aws
13M /usr/src/linux-headers-4.4.0-1060-aws

df command output:

df -k
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              499316       0    499316   0% /dev
tmpfs             101444   10740     90704  11% /run
/dev/xvda1       8065444 8049060         0 100% /
tmpfs             507212       0    507212   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             507212       0    507212   0% /sys/fs/cgroup
/dev/xvdf        5029504   10244   4740732   1% /data
tmpfs             101444       0    101444   0% /run/user/1000

Answer

To delete linux headers safely, always use:

$ sudo apt-get autoremove

Sometimes this command can give error like this:

Reading package lists... Error!
E: Could not create temporary file for /var/cache... (28: No space left on device)
E: The package lists or status file could not be parsed or opened.

In this case, delete one or two old headers manually. To do this first go to /usr/src folder and list the files according to date (oldest last) using below commands:

$ cd /usr/src
$ ls -lt

Then choose an old header (e.g. linux-aws-headers-4.4.0-1067) and delete it:

$ sudo rm -r linux-aws-headers-4.4.0-1067

After doing this, the command sudo apt-get autoremove should work without any problem.