Horrible Solution: How to delete all docker logs 2


Recently, we needed to delete the logs of a running docker setup. To do so, we used the following horrible solution:

First, we executed docker system info | grep "Docker Root Dir";to get the installation path of docker. It resulted to the following:

Docker Root Dir: /var/lib/docker

Then, we truncated all log files using the following command while executing as root:

truncate -s 0 /var/lib/docker/containers/*/*-json.log;

Bonus

To make the command a onliner, use the following:


sudo sh -c 'truncate -s 0 $(docker system info | grep "Docker Root Dir" | cut -d ":" -f2 | cut -d " " -f2-)/containers/*/*-json.log';

This post is also available in: Greek


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 thoughts on “Horrible Solution: How to delete all docker logs

  • codevarun

    This didn’t work for me – 🙂

    ➜ ~ sudo sh -c ‘truncate -s 0 $(docker system info | grep “Docker Root Dir” | cut -d “:” -f2 | cut -d ” ” -f2-)/containers/*/*-json.log’;
    WARNING: No swap limit support
    sh: 1: truncate: Argument list too long

  • Guy Bertental

    I got an error when i tried to run this:

    sudo truncate -s 0 /var/lib/docker/containers/**/*-json.log

    error:

    truncate: cannot open ‘/var/lib/docker/containers/**/*-json.log’ for writing: No such file or directory

    but i’ve managed to perform this in another way:

    sudo find /var/lib/docker/containers/ *-json.log -exec truncate -s 0 {} +

    just getting errors on the folders, but it works:

    truncate: cannot open ‘/var/lib/docker/containers/c2af5c83738652ce793e277a366f68c33a2a97dcc5d763dafbb0f8a736adb5ed’ for writing: Is a directory