> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Flushing events

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains how to use the flush feature. The flush feature is available in server-side, client-side, and edge SDKs.

Server-side, client-side, and edge SDKs automatically flush pending analytics events to LaunchDarkly at regular intervals. This prevents the SDK from having to send constant network requests. The time between intervals varies by SDK, and is configurable. To learn how to configure your SDK's flush interval, read [SDK configuration](/docs/sdk/features/config).

You can manually call flush to send events immediately without waiting for the next interval. Most customers do not need to use the flush feature because SDKs automatically flush their pending analytics events on a periodic frequency. However, it can be useful if you are using the SDK in a short-lived serverless process or a test application, rather than in a long-running application. To learn more, read [Analytics events](/docs/sdk/concepts/events).

## About the flush feature

The flush feature tells the client to send all of an SDK's pending analytics events to LaunchDarkly as soon as possible.

All SDKs support asynchronous flushing, which tells the SDK to start delivering events, but returns control to the application before delivery is complete. Some SDKs also support synchronous flushing, which tells the SDK to deliver the events and not return control until delivery is complete.

Details about each SDK's configuration are available in the SDK-specific sections below.

* [Client-side SDKs](#client-side-sdks)
* [Server-side SDKs](#server-side-sdks)
* [Edge SDKs](#edge-sdks)

## Client-side SDKs

This feature is available in the following client-side SDKs:

* [.NET (client-side)](#net-client-side)
* [Android](#android)
* [C++ (client-side)](#c++-client-side)
* [Flutter](#flutter)
* [iOS](#ios)
* [JavaScript](#javascript)
* [Node.js (client-side)](#node-js-client-side)
* [React Native](#react-native)
* [Roku](#roku)
* [Vue](#vue): The Vue SDK relies on the JavaScript SDK for this functionality.

### .NET (client-side)

<Accordion title="Expand .NET (client-side) code sample">
  Internally, the client-side .NET SDK keeps an event buffer for `Track` and `Identify` calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call `Flush` to process events immediately.

  The flush interval is configurable. If you need to change the interval, you can do so with the `Configuration` class.

  To call `flush`:

  <CodeGroup>
    ```csharp title="C#" lines wrap theme={null}
    client.Flush();
    ```
  </CodeGroup>
</Accordion>

### Android

<Accordion title="Expand Android code sample">
  The Android SDK keeps an internal event buffer for analytics calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

  Here's how:

  <CodeGroup>
    ```java title="Java" lines wrap theme={null}
    client.flush();
    ```

    ```kotlin title="Kotlin" lines wrap theme={null}
    client.flush()
    ```
  </CodeGroup>

  The flush interval is configurable.
</Accordion>

### C++ (client-side)

<Accordion title="Expand C++ (client-side) code sample">
  The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. In some situations, for example if you're testing the SDK in a simulator, you may want to manually call flush to process events immediately.

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" lines wrap theme={null}
    client.FlushAsync();
    ```

    ```c title="C++ SDK v3.0 (C binding)" lines wrap theme={null}
    LDClientSDK_Flush(client, LD_NONBLOCKING);
    ```
  </CodeGroup>

  You can also examine the result to determine if the flush succeeded. Here's how:

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" lines wrap theme={null}
    /* FlushAsync returns immediately; event delivery proceeds in the
       background. The call does not report a result. */
    client.FlushAsync();
    ```

    ```cpp title="C++ SDK v3.0 (C binding)" lines wrap theme={null}
    /* LDClientSDK_Flush returns immediately; event delivery proceeds in
       the background. The second parameter must be LD_NONBLOCKING, and the
       call does not report a result. */
    LDClientSDK_Flush(client, LD_NONBLOCKING);
    ```
  </CodeGroup>

  The flush interval is configurable. If you need to change the interval, you can do so using the configuration. To learn more, read [`FlushAsync`](https://launchdarkly.github.io/cpp-sdks/libs/client-sdk/docs/html/classlaunchdarkly_1_1client__side_1_1Client.html#ad19e6199bff8c0c0caa94e28f44ded3f) and [`FlushInterval`](https://launchdarkly.github.io/cpp-sdks/libs/client-sdk/docs/html/classlaunchdarkly_1_1config_1_1shared_1_1builders_1_1EventsBuilder.html#a999ee341dcf36b412e14ec5c923cceff).
</Accordion>

### Flutter

<Accordion title="Expand Flutter code sample">
  Internally, the Flutter SDK keeps an event buffer for `track` calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

  To call `flush`:

  <CodeGroup>
    ```dart title="Dart" lines wrap theme={null}
    await client.flush();
    ```
  </CodeGroup>

  To learn more, read [`flush`](https://pub.dev/documentation/launchdarkly_flutter_client_sdk/latest/launchdarkly_flutter_client_sdk/LDClient/flush.html).
</Accordion>

### iOS

<Accordion title="Expand iOS code sample">
  Internally, the iOS SDK keeps an event buffer for `track` calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

  To call `flush`:

  <CodeGroup>
    ```swift title="Swift" lines wrap theme={null}
    LDClient.get()!.flush()

    ```

    ```objectivec title="Objective-C" lines wrap theme={null}
    [[LDClient get] flush];
    ```
  </CodeGroup>
</Accordion>

### JavaScript

<Accordion title="Expand JavaScript code sample">
  Internally, the JavaScript SDK keeps an event buffer for `track` calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

  This method is asynchronous. You can pass a callback or wait for the returned `Promise` to determine when all events have been flushed.

  To call `flush`:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    client.flush();
    ```
  </CodeGroup>
</Accordion>

### Node.js (client-side)

<Accordion title="Expand Node.js (client-side) code sample">
  Internally, the LaunchDarkly SDK keeps an analytics event buffer. These events are flushed periodically. In some situations, you may want to manually call `flush` to process events immediately.

  This method is asynchronous. You can pass a callback or wait for the returned `Promise` to determine when all events have been flushed.

  To call `flush`:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    client.flush();

    // or, with a callback:
    client.flush(() => {
      console.log('flush complete');
    });

    // or, with a Promise:
    client.flush().then(() => {
      console.log('flush complete');
    });
    ```
  </CodeGroup>
</Accordion>

### React Native

<Accordion title="Expand React Native code sample">
  Internally, the React Native SDK keeps an event buffer for `track` calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately.

  To call `flush`:

  <CodeGroup>
    ```js title="React Native SDK, v10" lines wrap theme={null}
    const { result, error } = await client.flush();
    ```
  </CodeGroup>

  `flush` is asynchronous and can be awaited. It returns a promise that resolves to an object containing an error,  if there is one, and a boolean result.

  To learn more, read [`flush`](https://launchdarkly.github.io/js-core/packages/sdk/react-native/docs/classes/LDClientImpl.html#flush).
</Accordion>

### Roku

<Accordion title="Expand Roku code sample">
  Internally, the Roku SDK keeps an event buffer for `track` calls. These are flushed periodically in a background thread. You can configure the flush interval if needed.

  In some situations, such as when you're testing the SDK in a simulator, you may want to manually call `flush` to request any queued events to be sent immediately. This call is non-blocking, so it returns before the events are sent.

  To call `flush`:

  <CodeGroup>
    ```brightscript title="BrightScript" lines wrap theme={null}
    launchDarkly.flush()

    ```
  </CodeGroup>
</Accordion>

### Vue

<Accordion title="Expand Vue code sample">
  The Vue SDK relies on the [JavaScript SDK](#javascript) for flushing events. Retrieve the client with `useLDClient()` and call the `flush()` method.

  Here's how:

  <CodeGroup>
    ```html title="Vue SDK v3.0" lines wrap theme={null}
    <script setup>
    import { useLDClient } from '@launchdarkly/vue-client-sdk'

    const ldClient = useLDClient()

    async function flushEvents() {
      await ldClient.flush()
    }
    </script>
    ```
  </CodeGroup>
</Accordion>

## Server-side SDKs

This feature is available in the following server-side SDKs:

* [.NET (server-side)](#net-server-side)
* [C++ (server-side)](#c++-server-side)
* [Go](#go)
* [Haskell](#haskell)
* [Java](#java)
* [Lua](#lua)
* [Node.js (server-side)](#node-js-server-side)
* [PHP](#php)
* [Python](#python)
* [Ruby](#ruby)
* [Rust](#rust)

### .NET (server-side)

<Accordion title="Expand .NET (server-side) code sample">
  The .NET (server-side) SDK supports asynchronous flushing with the [`Flush`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_Flush_) method.

  To call `flush`:

  <CodeGroup>
    ```csharp title="C#" lines wrap theme={null}
    client.Flush();
    ```
  </CodeGroup>

  Starting in version 7.0.0, the SDK also supports synchronous flushing with the [`FlushAndWait`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_FlushAndWait_System_TimeSpan_) method. In this example, the `TimeSpan.FromSeconds(2)` value means that the application is willing to wait no more than two seconds for event delivery.

  <CodeGroup>
    ```csharp title="C#" lines wrap theme={null}
    client.FlushAndWait(TimeSpan.FromSeconds(2));
    ```
  </CodeGroup>

  The flush interval is configurable. If you need to change the interval, you can do so with the `Configuration` class.

  Here's how:

  <CodeGroup>
    ```csharp title="C#" lines wrap theme={null}
    var config = Configuration.Builder("YOUR_SDK_KEY")
        .Events(
            Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(10))
        )
        .Build();
    var client = new LdClient(config);

    ```
  </CodeGroup>
</Accordion>

### C++ (server-side)

<Accordion title="Expand C++ (server-side) code sample">
  The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

  This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

  Here's how:

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" lines wrap theme={null}
    client.FlushAsync();
    ```

    ```c title="C++ SDK v3.0 (C binding)" lines wrap theme={null}
    LDServerSDK_Flush(client, LD_NONBLOCKING);
    ```
  </CodeGroup>

  The flush interval is configurable. If you need to change the interval, you can do so using the configuration. To learn more, read about `FlushAsync()` in [`Client`](https://launchdarkly.github.io/cpp-sdks/libs/server-sdk/docs/html/classlaunchdarkly_1_1server__side_1_1Client.html) and about `FlushInterval()` in [`EventsBuilder`](https://launchdarkly.github.io/cpp-sdks/libs/server-sdk/docs/html/classlaunchdarkly_1_1config_1_1shared_1_1builders_1_1EventsBuilder.html).
</Accordion>

### Go

<Accordion title="Expand Go code sample">
  The Go SDK supports asynchronous flushing with the [`Flush`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDClient.Flush) method.

  <CodeGroup>
    ```go title="Go" lines wrap theme={null}
    client.Flush();
    ```
  </CodeGroup>

  Starting in version 6.0.0, the SDK also supports synchronous flushing with the [`FlushAndWait`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDClient.FlushAndWait) method. In this example, the `time.Second*2` value means that the application is willing to wait no more than two seconds for event delivery.

  <CodeGroup>
    ```go title="Go" lines wrap theme={null}
    client.FlushAndWait(time.Second*2);
    ```
  </CodeGroup>

  The interval for automatic event flushing is configurable. If you need to change the interval, you can do so by making a custom client configuration. Here's how:

  <CodeGroup>
    ```go title="Go" lines wrap theme={null}
    config := ld.Config{
        Events: ldcomponents.SendEvents().FlushInterval(time.Second*10),
    }
    ```
  </CodeGroup>
</Accordion>

### Haskell

<Accordion title="Expand Haskell code sample">
  The LaunchDarkly SDK keeps an internal event buffer for analytics events. These events are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

  This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

  Here's how:

  <CodeGroup>
    ```haskell title="Haskell" lines wrap theme={null}
    flushEvents client
    ```
  </CodeGroup>
</Accordion>

### Java

<Accordion title="Expand Java code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for `track` and `identify` calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call [`flush`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#flush--) to process events immediately.

  Here's how:

  <CodeGroup>
    ```java title="Java" lines wrap theme={null}
    client.flush();
    ```
  </CodeGroup>

  The flush interval is configurable. If you need to change the interval, you can do so with [`LDConfig.Builder`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDConfig.Builder.html) and [`Components.sendEvents()`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/Components.html#sendEvents--).
</Accordion>

### Lua

<Accordion title="Expand Lua code sample">
  The LaunchDarkly SDK keeps an internal event buffer for analytics events. These are flushed periodically in a background thread. If you test the SDK in a simulator, you may want to manually call flush to process events immediately.

  This function will not block, but instead initiate a flush operation in the background. The flush interval is configurable. If you need to change the interval, you can do so with the configuration.

  Here's how:

  <CodeGroup>
    ```lua title="Lua" lines wrap theme={null}
    client:flush()
    ```
  </CodeGroup>

  To learn more, read [`flush`](https://launchdarkly.github.io/lua-server-sdk/modules/launchdarkly-server-sdk.html#flush).
</Accordion>

### Node.js (server-side)

<Accordion title="Expand Node.js (server-side) code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for `track` and `identify` calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call `flush` to process events immediately.

  The flush interval is configurable. If you need to change the interval, you can do so when configuring your client instance.

  Here's how:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    client.flush();
    ```
  </CodeGroup>
</Accordion>

### PHP

<Accordion title="Expand PHP code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for `variation`, `track`, and `identify` calls. These are automatically flushed when the LDClient is destroyed. PHP's shared-nothing architecture means manual invocation of this method is typically not needed. Developers may do so if they wish to flush events prior to teardown.

  Here's how:

  <CodeGroup>
    ```php title="PHP" lines wrap theme={null}
    $ldclient->flush();
    ```
  </CodeGroup>
</Accordion>

### Python

<Accordion title="Expand Python code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for `variation`, `track`, and `identify` calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call `flush` to process events immediately. Otherwise, Python may close before flushing the event buffer and your user changes and tracks will be lost.

  The flush interval is configurable. If you need to change the interval, you can do so when you configure your client instance.

  Here's how:

  <CodeGroup>
    ```python title="Python" lines wrap theme={null}
    ldclient.get().flush()
    ```
  </CodeGroup>
</Accordion>

### Ruby

<Accordion title="Expand Ruby code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for `track` and `identify` calls. These are flushed periodically in a background thread. If you test the SDK in a REPL, you may want to manually call `flush` to process events immediately.

  The flush interval is configurable. If you need to change the interval, you can do so when you configure your client instance.

  Here's how:

  <CodeGroup>
    ```ruby title="Ruby" lines wrap theme={null}
    client.flush
    ```
  </CodeGroup>
</Accordion>

### Rust

<Accordion title="Expand Rust code sample">
  Internally, the LaunchDarkly SDK keeps an event buffer for the analytics events that are produced by calling the `variation` or `variation_detail` methods, the `track` methods, or `identify`. These are flushed periodically in a background thread. In some situations, you may want to manually call [`flush`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.flush) to process events immediately.

  The Rust SDK supports asynchronous flushing with the `flush` method:

  <CodeGroup>
    ```rust title="Rust" lines wrap theme={null}
    let result = client.flush();
    ```
  </CodeGroup>

  Starting in version 3.0, the SDK also supports synchronous flushing with the `flush_blocking` method. This method triggers a flush of events currently in the outbox and blocks until that flush completes or the specified timeout expires. It returns `true` if the flush completed, or `false` if the timeout was reached. In this example, the `Duration::from_secs(5)` value means that the application is willing to wait no more than five seconds for the flush to complete.

  <CodeGroup>
    ```rust title="Rust SDK v3.0" lines wrap theme={null}
    use std::time::Duration;

    let success = client.flush_blocking(Duration::from_secs(5)).await;
    if !success {
        eprintln!("Event flush timed out");
    }
    ```
  </CodeGroup>

  The flush interval is configurable. If you need to change the interval, you can do so by making a custom client configuration.
</Accordion>

## Edge SDKs

Most edge SDKs support sending events directly. The [Akamai SDK](/docs/sdk/edge/akamai) does not.

When you [configure an edge SDK to send events](/docs/sdk/features/config#edge-sdks), you must also flush those events to ensure that they are sent back to LaunchDarkly.

This feature is available in the following edge SDKs:

* [Cloudflare](#cloudflare)
* [Fastly](#fastly)
* [Vercel](#vercel)

### Cloudflare

<Accordion title="Expand Cloudflare code sample">
  Flushing events is available in Cloudflare SDK version 2.3.0 and later.

  If you send [events](/docs/sdk/features/events#cloudflare), you must also flush those events before your worker exits to ensure that they are sent back to LaunchDarkly.

  If you call `flush` inside the `waitUntil` method, then flushing events will not impact the handler's response time. To learn more, read the [Cloudflare documentation on `waitUntil`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil).

  Here's how:

  <CodeGroup>
    ```ts title="Cloudflare SDK v2.3.0+ (TypeScript)" lines wrap theme={null}
    // executionContext is the Cloudflare worker handler context
    // https://github.com/cloudflare/workers-types/blob/master/index.d.ts#L567
    executionContext.waitUntil(
      client.flush((err, res) => {
        console.log(`flushed events result: ${res}, error: ${err}`);
      }),
    );
    ```
  </CodeGroup>

  If you do not call `flush`, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.
</Accordion>

### Fastly

<Accordion title="Expand Fastly code sample">
  If you send [events](/docs/sdk/features/events#fastly), you must also flush those events before your worker exits to ensure that they are sent back to LaunchDarkly.

  If you call `flush` inside the `waitUntil` method, then flushing events will not impact the handler's response time. To learn more, read the [Fastly Compute documentation on `waitUntil`](https://js-compute-reference-docs.edgecompute.app/docs/globals/FetchEvent/prototype/waitUntil).

  Here's how:

  <CodeGroup>
    ```ts title="TypeScript" lines wrap theme={null}
    async function handleRequest(event: FetchEvent) {

      // ...

      event.waitUntil(
        client.flush((err, res) => {
          console.log(`flushed events result: ${res}, error: ${err}`);
        }),
      );

      // ...
    }
    ```
  </CodeGroup>

  If you do not call `flush`, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.

  To learn more, read [`flush`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#flush).
</Accordion>

### Vercel

<Accordion title="Expand Vercel code sample">
  Flushing events is available in Vercel SDK v1.2.0 and later.

  If you send [events](/docs/sdk/features/events#vercel), you must also flush those events before your worker exits to ensure that they are sent back to LaunchDarkly.

  If you call `flush` inside the `waitUntil` method, then flushing events will not impact the handler's response time. To learn more, read the [Vercel documentation on `waitUntil`](https://vercel.com/docs/functions/edge-functions/edge-functions-api#waituntil).

  Here's how:

  <CodeGroup>
    ```ts title="Vercel SDK v1.2.0+ (TypeScript)" lines wrap theme={null}
    context.waitUntil(
      client.flush((err, res) => {
        console.log(`flushed events result: ${res}, error: ${err}`);
      }),
    );
    ```
  </CodeGroup>

  If you do not call `flush`, the events will not be sent to LaunchDarkly servers, due to the ephemeral nature of edge workers.
</Accordion>
