---
title: "Socket.destroy() | Grafana k6 documentation"
description: "Close and destroy the TCP socket"
---

# Socket.destroy()

Closes the connection and destroys the socket. After you call `destroy()`, the socket transitions to the `'destroyed'` state, and you can’t reuse it. A `close` event is emitted after the socket is destroyed.

## Signature

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
socket.destroy()
```

## Parameters

None.

## Returns

`void`

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import { Socket } from "k6/x/tcp"

export default async function () {
  const socket = new Socket()

  const closed = new Promise((resolve) => {
    socket.on("close", () => {
      console.log("Connection closed")
      resolve()
    })
  })

  socket.on("data", (data) => {
    const str = String.fromCharCode.apply(null, new Uint8Array(data))
    console.log("Received:", str)
    socket.destroy()
  })

  socket.on("error", (err) => {
    console.error("Error:", err)
    socket.destroy()
  })

  await socket.connect(8080, "example.com")
  await socket.write("Hello, server!")

  await closed
}
```
