Skip to content

Commit 2048073

Browse files
committed
Table cleanup
1 parent 882f06c commit 2048073

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

CIPPTimers.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,13 @@
142142
"Priority": 15,
143143
"RunOnProcessor": true,
144144
"IsSystem": true
145+
},
146+
{
147+
"Command": "Start-TableCleanup",
148+
"Description": "Timer to cleanup tables",
149+
"Cron": "0 0 23 * * *",
150+
"Priority": 20,
151+
"RunOnProcessor": true,
152+
"IsSystem": true
145153
}
146154
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function Start-TableCleanup {
2+
<#
3+
.SYNOPSIS
4+
Start the Table Cleanup Timer
5+
#>
6+
[CmdletBinding(SupportsShouldProcess = $true)]
7+
param()
8+
9+
$CleanupRules = @(
10+
@{
11+
DataTableProps = @{
12+
Context = (Get-CIPPTable -tablename 'webhookTable').Context
13+
Property = @('PartitionKey', 'RowKey', 'ETag', 'Resource')
14+
}
15+
Where = "`$_.Resource -match '^Audit'"
16+
}
17+
@{
18+
DataTableProps = @{
19+
Context = (Get-CIPPTable -tablename 'AuditLogSearches').Context
20+
Filter = "Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
21+
First = 10000
22+
Property = @('PartitionKey', 'RowKey', 'ETag')
23+
}
24+
}
25+
@{
26+
DataTableProps = @{
27+
Context = (Get-CIPPTable -tablename 'CippFunctionStats').Context
28+
Filter = "Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
29+
First = 10000
30+
Property = @('PartitionKey', 'RowKey', 'ETag')
31+
}
32+
}
33+
@{
34+
DataTableProps = @{
35+
Context = (Get-CIPPTable -tablename 'CippQueue').Context
36+
Filter = "Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
37+
First = 10000
38+
Property = @('PartitionKey', 'RowKey', 'ETag')
39+
}
40+
}
41+
@{
42+
DataTableProps = @{
43+
Context = (Get-CIPPTable -tablename 'CippQueueTasks').Context
44+
Filter = "Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
45+
First = 10000
46+
Property = @('PartitionKey', 'RowKey', 'ETag')
47+
}
48+
}
49+
)
50+
51+
if ($PSCmdlet.ShouldProcess('Start-TableCleanup', 'Starting Table Cleanup')) {
52+
Write-Information 'Starting table cleanup'
53+
foreach ($Rule in $CleanupRules) {
54+
if ($Rule.Where) {
55+
$Where = [scriptblock]::Create($Rule.Where)
56+
} else {
57+
$Where = { $true }
58+
}
59+
$DataTableProps = $Rule.DataTableProps
60+
61+
$CleanupCompleted = $false
62+
do {
63+
$Entities = Get-AzDataTableEntity @DataTableProps | Where-Object $Where
64+
if ($Entities) {
65+
Write-Information "Removing $($Entities.Count) entities from $($Rule.DataTableProps.Context.TableName)"
66+
try {
67+
Remove-AzDataTableEntity -Context $DataTableProps.Context -Entity $Entities -Force
68+
if ($DataTableProps.First -and $Entities.Count -lt $DataTableProps.First) {
69+
$CleanupCompleted = $true
70+
}
71+
} catch {
72+
Write-LogMessage -API 'TableCleanup' -message "Failed to remove entities from $($DataTableProps.Context.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
73+
$CleanupCompleted = $true
74+
}
75+
} else {
76+
$CleanupCompleted = $true
77+
}
78+
} while (!$CleanupCompleted)
79+
}
80+
Write-Information 'Table cleanup complete'
81+
}
82+
}

0 commit comments

Comments
 (0)