Delete documents (Go)

Finds documents in a collection using filter clauses, and then deletes those documents.

If you don’t use a filter, or if you use an empty filter, then this method deletes all documents in the collection.

This method does not support sort parameters, including vector search. If you want to use sort parameters to find documents, use the method to find documents instead. Then, use the method to delete a single document to delete each found document by ID.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Deletes documents that match the specified parameters and returns a *DeleteResult struct that includes the number of documents that were deleted.

A result that includes a deleted count of -1 indicates that every document in the collection was deleted.

Parameters

Use the DeleteMany method, which belongs to the Collection type.

Method signature
func (c *Collection) DeleteMany(
  ctx context.Context,
  f CollectionFilter,
  opts ...options.CollectionDeleteManyOption
) (*results.DeleteResult, error)
Name Type Summary

ctx

context.Context

The context for the operation.

f

CollectionFilter

An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes.

You must use & to escape any . or & in field names in the filter clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (Go).

For a list of available filter operators and more examples, see Filter operators for collections (Go).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

opts

…​options.CollectionDeleteManyOption

Optional. A builder to generate options for this operation. See Methods of the CollectionDeleteManyOption builder for more details.

Methods of the CollectionDeleteManyOption builder
Method Summary

SetTimeout(v time.Duration)

Optional. The maximum time, in milliseconds, that the whole operation can take. The whole operation can involve multiple HTTP requests. Overrides the GeneralMethod from the hierarchy. For control over other timeouts, use the UpdateAPIOptions method.

UpdateAPIOptions(v …​APIOption)

Optional. General API options for this operation, including the timeout.

Examples

The following examples demonstrate how to delete documents in a collection.

Delete documents that match a filter

You can use a filter to find documents that match specific criteria. For example, you can find documents with an is_checked_out value of false and a number_of_pages value less than 300.

For a list of available filter operators and more examples, see Filter operators for collections (Go).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/filter"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Delete documents
	result, err := collection.DeleteMany(
		ctx,
		filter.And(
			filter.Eq("is_checked_out", false),
			filter.Lt("number_of_pages", 300),
		),
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)
}

Delete all documents

To delete all documents, use an empty filter.

A result that includes a deleted count of -1 indicates that every document in the collection was deleted.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/filter"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Delete all documents
	result, err := collection.DeleteMany(
		ctx,
		filter.F{}, // explicitly use an empty filter instead of nil
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)
}

Delete more than 20 documents

The client automatically issues multiple Data API HTTP requests to delete all documents that match your filter.

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM