Solving problems invented by others...
Delete old logfiles

Delete old logfiles

These days i had to serve in our hotline service. I got a incident where the hard disk of a server was completely full. The cause was a improper working application which writes tons of small logfiles (1-3 kb each) into a directory.

While searching for the place where alle the storage was wasted, i identified the directory and tried to access it with Windows Explorer. But because there were over 350.000 files in this directory the explorer.exe process got stuck. I was unable to simply show the content. So i couldn´t sort the files after date of creation and select the oldest one for deletion. The additional problem was that the application writes 4-10 new files per minute which causes the Explorer to reload the directory content again.

So i had to find another way to delete these old files. Thanks to PowerShell it took me only 5 Minutes to find the right command.

Get-ChildItem -Path "c:\ProgramData\ApplicationName\logs" | Where-Object -FilterScript { $_.CreationTime -lt (Get-Date).AddMonths(-1) } | Remove-Item

This deletes all files older than one month in folder “c:\ProgramData\ApplicationName\logs”.

Nevertheless it took more than 20 minutes to delete over 300.000 files that way.

I love these PowerShell one liners…

Leave a Reply

Your email address will not be published. Required fields are marked *

three × = twelve