user9975267
user9975267

Reputation: 11

Efficient way to delete old log files on Windows 16 server?

I have over 10 TB of log file data. I want to find an efficient way to delete log files that are older than 3 years old. Originally, I was thinking about scheduling this PowerShell script:

# Delete all Files in PATH older than 3 years (1095 days) old
$Path = "Path to log files"
$Daysback = "-1095"

$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

I am concerned that looping through 10 TB of data will take hours to run even thought this script appears to run on O(n). Does anybody have any better solution?

Upvotes: 1

Views: 644

Answers (1)

Mike Twc
Mike Twc

Reputation: 2355

If performance matters I'd get rid of pipes and wouldn't use gci (use lower level types from System.IO). If there are millions of files I'd also write it in c# and only call methods from ps runtime. Below is example to remove files older than N days:

Add-Type -TypeDefinition @"

public static class LogCleaner {

  public static void Clean(string dirName, int age)
  {

    foreach (string file in System.IO.Directory.GetFiles(dirName))
    {
      var fi = new System.IO.FileInfo(file);
      if (fi.LastWriteTime < System.DateTime.Now.AddDays(-age))
      {
        fi.Delete();
      }
    }
  }

}

"@

[LogCleaner]::Clean("C:\Temp\demo", 10)

You may also check how to achive this using windows native utilities (like dir), or maybe install git bash and try to do this linux way (e.g. using find command)

Upvotes: 1

Related Questions