Skip to content

Commit ebef551

Browse files
committed
chore: refactor cache cleanups
1 parent 37c94ac commit ebef551

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-ListTenants.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Function Invoke-ListTenants {
2323
}
2424

2525
# Clear Cache
26-
if ($Request.Query.ClearCache -eq $true) {
27-
Remove-CIPPCache -tenantsOnly $Request.Query.TenantsOnly
26+
if ($Request.Body.ClearCache -eq $true) {
27+
$Results = Remove-CIPPCache -tenantsOnly $Request.Body.TenantsOnly
2828

2929
$InputObject = [PSCustomObject]@{
3030
Batch = @(
@@ -40,7 +40,12 @@ Function Invoke-ListTenants {
4040
$GraphRequest = [pscustomobject]@{'Results' = 'Cache has been cleared and a tenant refresh is queued.' }
4141
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
4242
StatusCode = [HttpStatusCode]::OK
43-
Body = $GraphRequest
43+
Body = @{
44+
Results = @($GraphRequest)
45+
Metadata = @{
46+
Details = $Results
47+
}
48+
}
4449
})
4550
#Get-Tenants -IncludeAll -TriggerRefresh
4651
return

Modules/CIPPCore/Public/Entrypoints/Timer Functions/Start-TableCleanup.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,27 @@ function Start-TableCleanup {
4848
}
4949
)
5050

51+
$DeleteTables = @(
52+
'knownlocationdb'
53+
)
54+
5155
if ($PSCmdlet.ShouldProcess('Start-TableCleanup', 'Starting Table Cleanup')) {
56+
foreach ($Table in $DeleteTables) {
57+
try {
58+
$Table = Get-CIPPTable -tablename $Table
59+
if ($Table) {
60+
Write-Information "Deleting table $($Table.Context.TableName)"
61+
try {
62+
Remove-AzDataTable -Context $Table.Context -Force
63+
} catch {
64+
Write-LogMessage -API 'TableCleanup' -message "Failed to delete table $($Table.Context.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
65+
}
66+
}
67+
} catch {
68+
Write-Information "Table $Table not found"
69+
}
70+
}
71+
5272
Write-Information 'Starting table cleanup'
5373
foreach ($Rule in $CleanupRules) {
5474
if ($Rule.Where) {

Modules/CIPPCore/Public/GraphHelper/Remove-CIPPCache.ps1

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,36 @@ function Remove-CIPPCache {
1313
if ($ClearIncludedTenants) {
1414
Remove-AzDataTableEntity -Force @TenantsTable -Entity $ClearIncludedTenants
1515
}
16+
"Removed $($ClearIncludedTenants.Count) tenants"
1617

17-
if ($TenantsOnly -eq 'false') {
18-
Write-Host 'Clearing all'
18+
if ($TenantsOnly -eq $false) {
19+
'Clearing all cached table data'
20+
$Context = New-AzDataTableContext -ConnectionString $env:AzureWebJobsStorage
21+
$Tables = Get-AzDataTable -Context $Context
22+
foreach ($Table in $Tables) {
23+
if ($Table -match '^cache') {
24+
"Removing cache table $Table"
25+
$TableContext = Get-CIPPTable -TableName $Table
26+
Remove-AzDataTable @TableContext
27+
}
28+
}
29+
30+
'Clearing domain analyser results'
1931
# Remove Domain Analyser cached results
2032
$DomainsTable = Get-CippTable -tablename 'Domains'
2133
$Filter = "PartitionKey eq 'TenantDomains'"
2234
$ClearDomainAnalyserRows = Get-CIPPAzDataTableEntity @DomainsTable -Filter $Filter | ForEach-Object {
23-
$_.DomainAnalyser = ''
35+
$_ | Add-Member -MemberType NoteProperty -Name DomainAnalyser -Value '' -Force
2436
$_
2537
}
2638
if ($ClearDomainAnalyserRows) {
2739
Update-AzDataTableEntity -Force @DomainsTable -Entity $ClearDomainAnalyserRows
2840
}
29-
#Clear BPA
30-
$BPATable = Get-CippTable -tablename 'cachebpav2'
31-
$ClearBPARows = Get-CIPPAzDataTableEntity @BPATable
32-
if ($ClearBPARows) {
33-
Remove-AzDataTableEntity -Force @BPATable -Entity $ClearBPARows
34-
}
41+
3542
$ENV:SetFromProfile = $null
3643
$Script:SkipListCache = $Null
3744
$Script:SkipListCacheEmpty = $Null
3845
$Script:IncludedTenantsCache = $Null
3946
}
47+
'Cache cleanup complete'
4048
}

0 commit comments

Comments
 (0)