PowerShell to shrink SharePoint auditdata table

I recently had a content database that was out of control size wise because of the Auditdata table and needed it shrunk promptly. This piece of PowerShell did the job.

NOTE: This is a bit of a hammer so all things look like a nail approach but it may be good for your specific situation.

if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {
   Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}

foreach ($site in get-spsite -Limit ALL)
{
   Write-host ‘Deleting audit data for site: ‘ $sc.URL
   $i = -350
   do {
      Write-Host $site.URL ‘ – Delete day ‘ $i ‘ : ‘ ([System.DateTime]::Now.ToLocalTime().AddDays($i))
      $site.audit.deleteentries([System.DateTime]::Now.ToLocalTime().AddDays($i))
      $site.audit.update()
      $i++
   }
   while ($i -le 1)
   $site.Dispose()
}