Remove-CosmosCollection.ps1
function Remove-CosmosCollection { <# .SYNOPSIS Removes a Cosmos Collection .DESCRIPTION Removes a Cosmos Collection and all documents it contains .PARAMETER DatabaseName Name of the Database containing the Collection you want to remove .PARAMETER CollectionName Name of the Collection you want to remove .PARAMETER CosmosDBVariables This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative .EXAMPLE Remove-CosmosCollection -DatabaseName MyPrivateCosmos -CollectionName TohuVaBohu .NOTES https://docs.microsoft.com/en-us/rest/api/documentdb/delete-a-collection #> [CmdletBinding()] param ( [Parameter(Mandatory=$true, HelpMessage='Name of your database')] [string]$DatabaseName, [Parameter(Mandatory=$true, HelpMessage='Name of the Collection to remove')] [string]$CollectionName, [Parameter(Mandatory=$false, HelpMessage="Use Connect-CosmosDB to create this Variable collection")] [hashtable]$CosmosDBVariables=$Script:CosmosDBVariables ) begin { Test-CosmosDBVariable $CosmosDBVariables } process { $Collection = $CosmosDBConnection[$DatabaseName][$CollectionName] if ($Collection) { $Verb = 'DELETE' $Url = '{0}/{1}' -f $URI,$Collection._self $ResourceType = 'colls' $Header = New-CosmosDBHeader -resourceId $Collection._rid -resourceType $ResourceType -Verb $Verb try { Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -ErrorAction Stop $Script:CosmosDBConnection.Remove($CollectionName) Write-Verbose "$CollectionName has been removed" } catch { Write-Warning -Message $_.Exception.Message } } else { Write-Warning "$CollectionName not found in $DatabaseName" } } end { } } |