truncate can be used to shrink or increase the size of a file. truncate with the option -s can be used to specify the size of the file. When you specify the size as 0, you can clear the contents of the file.
$ cat file.txt This file has some content. Let's use truncate to clear the file
Let’s clear the file contents
$ truncate -s 0 file.txt
If you specify the size as 0, the contents of the file will be cleared. Let’s confirm this
$ stat file.txt File: `file.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 1004 Links: 1
You can see more options to clear a file in Linux.