Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `Remove-FabricItem` confirms each item individually instead of once for the whole batch, so `-WhatIf` lists the items by name and ID and `-Confirm` prompts per item rather than authorising the entire batch with a single answer
- `Remove-FabricItem` reports item counts through `Write-Message` rather than `Write-Output`, so the count is no longer emitted into the pipeline as output

### Fixed

### Deprecated
Expand Down
77 changes: 52 additions & 25 deletions src/Public/Item/Remove-FabricItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@ Function Remove-FabricItem {
Removes selected items from a Fabric workspace.

.DESCRIPTION
The Remove-FabricItems function removes selected items from a specified Fabric workspace. It uses the workspace ID and an optional filter to select the items to remove. If a filter is provided, only items whose DisplayName matches the filter are removed.
The Remove-FabricItem function removes items from a specified Fabric workspace.

Supply `ItemID` to remove a single item. Otherwise the items in the workspace are listed and
removed; pass `Filter` to narrow that list to items whose DisplayName matches a wildcard
pattern. Without a filter, every item in the workspace is removed.

Each item is confirmed individually, so `-WhatIf` lists precisely what would be removed and
`-Confirm` prompts per item rather than once for the whole batch.

.PARAMETER WorkspaceID
The ID of the Fabric workspace. This is a mandatory parameter.

.PARAMETER Filter
An optional filter to select items to remove. If provided, only items whose DisplayName matches the filter are removed.
A wildcard pattern matched against each item's DisplayName. Only matching items are removed.
If omitted, every item in the workspace is removed.

.PARAMETER ItemID
The ID of a specific item to remove. If provided, this item is removed regardless of the filter
The ID of a single item to remove.

.EXAMPLE
This command removes all items from the workspace with the specified ID whose DisplayName includes "test". .INPUTS String. You can pipe two strings that contain the workspace ID and filter to Remove-FabricItems.
Removes every item in the workspace whose DisplayName contains "test".

```powershell
Remove-FabricItem -WorkspaceID "12345678-90ab-cdef-1234-567890abcdef" -Filter "*test*"
```

.EXAMPLE
Removes a single item by ID.

```powershell
Remove-FabricItem -WorkspaceID "12345678-90ab-cdef-1234-567890abcdef" -ItemID "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
```

.EXAMPLE
Lists every item that would be removed from the workspace, without removing anything.

```powershell
Remove-FabricItem -WorkspaceID "12345678-90ab-cdef-1234-567890abcdef" -WhatIf
```

.INPUTS
String. You can pipe a string that contains the workspace ID to Remove-FabricItem.

Expand All @@ -30,6 +52,11 @@ Function Remove-FabricItem {

.NOTES

Revision History:

- 2026-08-25 - PBO: Confirm each item individually so the prompt and -WhatIf name the item
being removed, rather than confirming the whole batch once.

Author: Rui Romano
https://github.com/microsoft/Analysis-Services/tree/master/pbidevmode/fabricps-pbip

Expand All @@ -47,27 +74,27 @@ Function Remove-FabricItem {

Confirm-TokenState

# Invoke the Fabric API to get the items in the workspace
if ($PSCmdlet.ShouldProcess("Remove items from workspace $workspaceId")) {
if ($itemID) {
Invoke-FabricRestMethod -Uri "workspaces/$($workspaceID)/items/$($itemID)" -Method Delete
} else {
$items = Invoke-FabricRestMethod -Uri "workspaces/$workspaceId/items" -Method Get

# Display the count of existing items
Write-Output "Existing items: $($items.Count)"

# If a filter is provided
if ($filter) {
# Filter the items whose DisplayName matches the filter
$items = $items | Where-Object { $_.DisplayName -like $filter }
}

# For each item
foreach ($item in $items) {
# Remove the item
Invoke-FabricRestMethod -Uri "workspaces/$workspaceId/items/$($item.ID)" -Method Delete
}
if ($itemID) {
if ($PSCmdlet.ShouldProcess("item $itemID in workspace $WorkspaceId", 'Remove')) {
Invoke-FabricRestMethod -Uri "workspaces/$($WorkspaceId)/items/$($itemID)" -Method Delete
}
return
}

$items = @(Invoke-FabricRestMethod -Uri "workspaces/$WorkspaceId/items" -Method Get)
Write-Message -Message "Workspace $WorkspaceId contains $($items.Count) item(s)." -Level Verbose

if ($filter) {
$items = @($items | Where-Object { $_.DisplayName -like $filter })
Write-Message -Message "$($items.Count) item(s) match filter '$filter'." -Level Info
} else {
Write-Message -Message "No filter specified - removing all $($items.Count) item(s) from workspace $WorkspaceId." -Level Info
}

foreach ($item in $items) {
$target = "'{0}' (id {1}) in workspace {2}" -f $item.displayName, $item.id, $WorkspaceId
if ($PSCmdlet.ShouldProcess($target, 'Remove')) {
Invoke-FabricRestMethod -Uri "workspaces/$WorkspaceId/items/$($item.id)" -Method Delete
}
}
}
51 changes: 51 additions & 0 deletions tests/Unit/Remove-FabricItem.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Describe "Remove-FabricItem" -Tag "UnitTests" {
It 'Should have the expected parameter: <Name>' -ForEach @(
@{ Name = 'workspaceId'; Mandatory = $true }
@{ Name = 'itemID'; Mandatory = $false }
@{ Name = 'filter'; Mandatory = $false }
) {
$command | Should -HaveParameter $Name -Mandatory:$Mandatory
}
Expand Down Expand Up @@ -102,4 +103,54 @@ Describe "Remove-FabricItem" -Tag "UnitTests" {
{ Remove-FabricItem -workspaceId $mockWorkspaceId -itemID $mockItemId -Confirm:$false } | Should -Throw
}
}

Context 'Deletion scope' {
BeforeAll {
Mock -CommandName Confirm-TokenState -MockWith { }
Mock -CommandName Write-Message -MockWith { }
Mock -CommandName Invoke-FabricRestMethod -MockWith {
if ($Method -eq 'Get') {
return @(
[pscustomobject]@{ id = '11111111-1111-1111-1111-111111111111'; displayName = 'keep-me' }
[pscustomobject]@{ id = '22222222-2222-2222-2222-222222222222'; displayName = 'test-one' }
[pscustomobject]@{ id = '33333333-3333-3333-3333-333333333333'; displayName = 'test-two' }
)
}
return $null
}
}

It 'Should delete only the items matching the filter' {
$mockWorkspaceId = [guid]::NewGuid()

Remove-FabricItem -workspaceId $mockWorkspaceId -filter 'test-*' -Confirm:$false

Should -Invoke -CommandName Invoke-FabricRestMethod -Times 2 -Exactly -ParameterFilter {
$Method -eq 'Delete'
}
Should -Invoke -CommandName Invoke-FabricRestMethod -Times 0 -Exactly -ParameterFilter {
$Method -eq 'Delete' -and $Uri -like '*11111111-1111-1111-1111-111111111111*'
} -Because 'keep-me does not match the filter'
}

It 'Should delete every item when no filter is supplied' {
$mockWorkspaceId = [guid]::NewGuid()

Remove-FabricItem -workspaceId $mockWorkspaceId -Confirm:$false

Should -Invoke -CommandName Invoke-FabricRestMethod -Times 3 -Exactly -ParameterFilter {
$Method -eq 'Delete'
}
}

It 'Should delete nothing under -WhatIf' {
$mockWorkspaceId = [guid]::NewGuid()

Remove-FabricItem -workspaceId $mockWorkspaceId -WhatIf

Should -Invoke -CommandName Invoke-FabricRestMethod -Times 0 -ParameterFilter {
$Method -eq 'Delete'
}
}
}
}
Loading