Octopus Deploy Documentation

Delete a Space

Last updated

This script deletes a Space from your Octopus instance.


Provide values for:

  • Octopus URL
  • Octopus API Key
  • Space Name to delete the space with the given name.

Be very careful when deleting a Space. This operation is destructive and cannot be undone.


$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "https://youroctourl"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

$spaceName = "New Space"

Write-Host "Getting space '$spaceName'"
$spaces = (Invoke-WebRequest $octopusURL/api/spaces?take=21000 -Headers $header -Method Get -ErrorVariable octoError).Content | ConvertFrom-Json

$space = $spaces.Items | Where-Object Name -eq $spaceName

if ($null -eq $space) {
    Write-Host "Could not find space with name '$spaceName'"

$space.TaskQueueStopped = $true
$body = $space | ConvertTo-Json

Write-Host "Stopping space task queue"
(Invoke-WebRequest $octopusURL/$($space.Links.Self) -Headers $header -Method PUT -Body $body -ErrorVariable octoError) | Out-Null

Write-Host "Deleting space"
(Invoke-WebRequest $octopusURL/$($space.Links.Self) -Headers $header -Method DELETE -ErrorVariable octoError) | Out-Null

Write-Host "Action Complete"
Add-Type -Path 'path\to\Octopus.Client.dll'

$octopusURL = "https://youroctourl"

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint($octopusURL, $octopusAPIKey)
$repository = New-Object Octopus.Client.OctopusRepository($endpoint)

$spaceName = "New Space"

$space = $repository.Spaces.FindByName($spaceName)

if ($null -eq $space) {
    Write-Host "The space $spaceName does not exist."

try {
    $space.TaskQueueStopped = $true

    $repository.Spaces.Modify($space) | Out-Null
    $repository.Spaces.Delete($space) | Out-Null
} catch {
    Write-Host $_.Exception.Message
// If using .net Core, be sure to add the NuGet package of System.Security.Permissions
#r "path\to\Octopus.Client.dll"

using Octopus.Client;
using Octopus.Client.Model;

var OctopusURL = "https://youroctourl";
var OctopusAPIKey = "API-YOURAPIKEY";

var endpoint = new OctopusServerEndpoint(OctopusURL, OctopusAPIKey);
var repository = new OctopusRepository(endpoint);

var spaceName = "New Space";

    Console.WriteLine($"Getting space '{spaceName}'.");
    var space = repository.Spaces.FindByName(spaceName);

    if (space == null)
        Console.WriteLine($"Could not find space '{spaceName}'.");

    Console.WriteLine("Stopping task queue.");
    space.TaskQueueStopped = true;


    Console.WriteLine("Deleting space");
catch (Exception ex)
import json
import requests

octopus_server_uri = 'https://your.octopus.app/api'
octopus_api_key = 'API-YOURAPIKEY'
headers = {'X-Octopus-ApiKey': octopus_api_key}

space_name = "Your Space name"

def get_octopus_resource(uri):
    response = requests.get(uri, headers=headers)

    return json.loads(response.content.decode('utf-8'))

def get_by_name(uri, name):
    resources = get_octopus_resource(uri)
    return next((x for x in resources if x['Name'] == name), None)

space = get_by_name('{0}/spaces/all'.format(octopus_server_uri), space_name)
space['TaskQueueStopped'] = True

# update task queue to stopped
uri = '{0}/spaces/{1}'.format(octopus_server_uri, space['Id'])
response = requests.put(uri, headers=headers, json=space)

# Delete space
response = requests.delete(uri, headers=headers)
package main

import (



func main() {

	apiURL, err := url.Parse("https://YourURL")
	if err != nil {
	APIKey := "API-YourAPIKey"
	spaceName := "MySpace"

	// Get reference to space
	space := GetSpace(apiURL, APIKey, spaceName)
	space.TaskQueueStopped = true

	// Create client object
	client := octopusAuth(apiURL, APIKey, "")

	deleteErr := client.Spaces.DeleteByID(space.ID)
	if deleteErr != nil {


func octopusAuth(octopusURL *url.URL, APIKey, space string) *octopusdeploy.Client {
	client, err := octopusdeploy.NewClient(nil, octopusURL, APIKey, space)
	if err != nil {

	return client

func GetSpace(octopusURL *url.URL, APIKey string, spaceName string) *octopusdeploy.Space {
	client := octopusAuth(octopusURL, APIKey, "")

	spaceQuery := octopusdeploy.SpacesQuery{
		Name: spaceName,

	// Get specific space object
	spaces, err := client.Spaces.Get(spaceQuery)

	if err != nil {

	for _, space := range spaces.Items {
		if space.Name == spaceName {
			return space

	return nil
import com.octopus.sdk.Repository;
import com.octopus.sdk.domain.Space;
import com.octopus.sdk.http.ConnectData;
import com.octopus.sdk.http.OctopusClient;
import com.octopus.sdk.http.OctopusClientFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Optional;

public class DeleteSpace {

  static final String octopusServerUrl = "http://localhost:8065";
  // as read from your profile in your Octopus Deploy server
  static final String apiKey = System.getenv("OCTOPUS_SERVER_API_KEY");

  public static void main(final String... args) throws IOException {
    final OctopusClient client = createClient();
    final Repository repo = new Repository(client);
    final Optional<Space> space = repo.spaces().getByName("TheSpaceName");
    if (!space.isPresent()) {
      System.out.println("No space named 'TheSpaceName' exists on server");

    final Space stoppedSpace = repo.spaces().update(space.get().getProperties());

  // Create an authenticated connection to your Octopus Deploy Server
  private static OctopusClient createClient() throws MalformedURLException {
    final Duration connectTimeout = Duration.ofSeconds(10L);
    final ConnectData connectData =
        new ConnectData(new URL(octopusServerUrl), apiKey, connectTimeout);
    final OctopusClient client = OctopusClientFactory.createClient(connectData);

    return client;

Need support? We're here to help.