# Delete segment

## API Overview

Segments are similar to contact lists, except they update dynamically over time as information stored about your contacts or the criteria used to define your segments changes. When you segment your audience, you are able to create personalized Automation emails and Single Sends that directly address the wants and needs of your particular audience.

The Marketing Campaigns Segments V2 API allows you to create, edit, and delete segments as well as retrieve a list of segments or an individual segment by ID.

Segments built using engagement data such as "was sent" or "clicked" will take approximately 30 minutes to begin populating.

Segment samples and counts refresh on a schedule that ranges from 1 to 24 hours. Segments with active automations or that are used as exclusion lists for scheduled Single Sends refresh every hour. Segments that aren't actively used refresh less frequently, up to every 24 hours, to optimize processing resources. Samples and counts displayed in the UI don't update immediately when segment criteria are modified or when contacts are added or updated. Instead, they update according to the segment's refresh schedule.

## Operation overview

```json
{"path":"https://api.sendgrid.com/v3/marketing/segments/2.0/{segment_id}","method":"delete","servers":[{"url":"https://api.sendgrid.com","description":"The Twilio SendGrid v3 API"}]}
```

**This endpoint allows you to delete a segment by ID.**

## Operation details

### Authentication

API Key

### Headers

```json
[{"in":"header","name":"Authorization","required":true,"default":"Bearer <<YOUR_API_KEY_HERE>>","schema":{"type":"string"}}]
```

### Path parameters

```json
[{"name":"segment_id","in":"path","required":true,"schema":{"type":"string"}}]
```

### Responses

```json
[{"responseCode":"202","schema":{"description":""}},{"responseCode":"400","schema":{"description":"","content":{"application/json":{"schema":{"title":"errors-seg","type":"object","description":"If the request is incorrect, an array of errors will be returned.","required":["errors"],"refName":"ErrorsSegmentV2","modelName":"ErrorsSegmentV2","properties":{"errors":{"type":"array","items":{"type":"object","required":["field","message"],"properties":{"field":{"type":"string","description":"the field in the request body that is incorrect"},"message":{"type":"string","description":"a description of what is specifically wrong with the field passed in the request"}}}}}}}}}},{"responseCode":"404","schema":{"description":""}},{"responseCode":"500","schema":{"description":"","content":{"application/json":{"schema":{"title":"errors-seg","type":"object","description":"If the request is incorrect, an array of errors will be returned.","required":["errors"],"refName":"ErrorsSegmentV2","modelName":"ErrorsSegmentV2","properties":{"errors":{"type":"array","items":{"type":"object","required":["field","message"],"properties":{"field":{"type":"string","description":"the field in the request body that is incorrect"},"message":{"type":"string","description":"a description of what is specifically wrong with the field passed in the request"}}}}}}}}}}]
```

Delete segment

```js
const client = require("@sendgrid/client");
client.setApiKey(process.env.SENDGRID_API_KEY);

const segment_id = "segment_id";

const request = {
  url: `/v3/marketing/segments/2.0/${segment_id}`,
  method: "DELETE",
};

client
  .request(request)
  .then(([response, body]) => {
    console.log(response.statusCode);
    console.log(response.body);
  })
  .catch((error) => {
    console.error(error);
  });
```

```python
import os
from sendgrid import SendGridAPIClient


sg = SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))

segment_id = "segment_id"

response = sg.client._(f"marketing/segments/2.0/{segment_id}").delete()

print(response.status_code)
print(response.body)
print(response.headers)
```

```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SendGrid;

public class Program {
    public static async Task Main() {
        string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        var client = new SendGridClient(apiKey);

        var segmentId = "segment_id";

        var response = await client.RequestAsync(
            method: SendGridClient.Method.DELETE, urlPath: $"marketing/segments/2.0/{segmentId}");

        Console.WriteLine(response.StatusCode);
        Console.WriteLine(response.Body.ReadAsStringAsync().Result);
        Console.WriteLine(response.Headers.ToString());
    }
}
```

```java
import com.sendgrid.*;
import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException {
        try {
            SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
            Request request = new Request();
            request.setMethod(Method.DELETE);
            request.setEndpoint("/marketing/segments/2.0/segment_id");
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            throw ex;
        }
    }
}
```

```go
package main

import (
	"fmt"
	"github.com/sendgrid/sendgrid-go"
	"os"
)

func main() {
	apiKey := os.Getenv("SENDGRID_API_KEY")
	host := "https://api.sendgrid.com"
	request := sendgrid.GetRequest(apiKey, "/v3/marketing/segments/2.0/segment_id", host)
	request.Method = "DELETE"
	response, err := sendgrid.API(request)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}
```

```php
<?php
// Uncomment the next line if you're using a dependency loader (such as Composer) (recommended)
// require 'vendor/autoload.php';

// Uncomment next line if you're not using a dependency loader (such as Composer)
// require_once '<PATH TO>/sendgrid-php.php';

$apiKey = getenv("SENDGRID_API_KEY");
$sg = new \SendGrid($apiKey);
$segment_id = "segment_id";

try {
    $response = $sg->client
        ->_("marketing/segments/2.0/{$segment_id}")
        ->delete();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $ex) {
    echo "Caught exception: " . $ex->getMessage();
}
```

```ruby
require 'sendgrid-ruby'
include SendGrid

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
segment_id = "segment_id"

response = sg.client._("marketing/segments/2.0/#{segment_id}").delete()
puts response.status_code
puts response.headers
puts response.body
```

```bash
curl -X DELETE "https://api.sendgrid.com/v3/marketing/segments/2.0/segment_id" \
--header "Authorization: Bearer $SENDGRID_API_KEY"
```
