Merge pull request #9239 from awanlin/topic/add-orphan-clean-up-script

Added Orphan Clean Up script
This commit is contained in:
Johan Haals
2022-01-31 14:39:26 +01:00
committed by GitHub
3 changed files with 46 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
# Scripts
Here you will find a variety of script contributions.
@@ -0,0 +1,22 @@
<#
.DESCRIPTION
Cleanes up orphaned entities for the provided Backstage URL, defaults to the local backend
#>
param(
[string]$backstageUrl = "http://localhost:7007"
)
$orphanApiUrl = "$backstageUrl/api/catalog/entities?filter=metadata.annotations.backstage.io/orphan=true"
$orphanDeleteApiUrl = "$backstageUrl/api/catalog/entities/by-uid"
$orphans = Invoke-RestMethod -Method Get -Uri $orphanApiUrl
Write-Host ""
Write-Host "Found $($orphans.length) orphaned entities"
Write-Host ""
foreach($orphan in $orphans){
Write-Host "Deleting orphan $($orphan.metadata.name) of kind $($orphan.kind)"
Invoke-RestMethod -Method Delete -Uri "$orphanDeleteApiUrl/$($orphan.metadata.uid)"
}
+21
View File
@@ -0,0 +1,21 @@
# Orphan Clean Up
## Overview
The Orphan Clean Up script is a basic PowerShell script to delete orphaned entities in the catalog. This script also assumes that you do not have authentication setup for your Backstage API endpoints.
_Warning:_ There is a risk of entities being orphaned (and being deleted by this script) in case of the location having problems and returning a 404 status code. This might lead to accidental deletion of entities until the processing loop has recreated the entity.
## Requirements
This script is PowerShell based so therefore needs to be ran in a PowerShell session. If you are not able to use PowerShell the script should give you a clear idea as to how to create it with another scripting language like Bash.
## Usage
Here's how to use the script:
1. Download the script
2. Now start a PowerShell session
3. Next navigate to the location you downloaded the script
4. Then run this command replacing `https:\\backstage.my-company.com` with the URL of your Backstage instance: `.\OrphanCleanUp.ps1 https:\\backstage.my-company.com`
5. The script will output the number of orphaned entities it finds and then the name for each one it deletes