# Bulk delete data

Use the managed bulk delete service to delete data from your database. This feature supports deletion against one or more collection groups.

## Managed bulk delete service

To delete documents in bulk, use the managed bulk delete service. The Cloud Firestore managed bulk delete service is available through the [`gcloud`](https://cloud.google.com/sdk/gcloud/) command-line tool and the Cloud Firestore [REST API](/content/docs/firestore/reference/rest/v1/projects.databases/bulkDeleteDocuments/index.html).

### Before you begin

Before you can use the managed bulk delete service, you must complete the following tasks:

1. [For bulk document deletion, enable billing for your Google Cloud project.](https://cloud.google.com/billing/docs/how-to/modify-project) Only Google Cloud projects with billing enabled can use the bulk delete functionality.

2. Make sure your account has the necessary permissions for Cloud Firestore. **If you are the project owner, your account has the required permissions.** Otherwise, the following roles grant the necessary permissions for bulk delete operations:
   - [Cloud Firestore roles:](https://cloud.google.com/firestore/docs/security/iam#predefined_roles) `Owner`, `Cloud Datastore Owner`, or `Cloud Datastore Bulk Admin`

#### Set up `gcloud` for your project

You can initiate bulk delete operations through the Google Cloud console or the `gcloud` command-line tool. To use `gcloud`, set up the command-line tool and connect to your project in one of the following ways:

- Access `gcloud` from the Google Cloud console using [Cloud Shell](https://cloud.google.com/shell/).

```
gcloud config set project [PROJECT_ID]
```

- [Install and initialize the Google Cloud SDK.](https://cloud.google.com/sdk/docs/quickstarts)

### Delete data with the managed bulk delete service

A bulk delete operation first finds all applicable documents in your database and deletes them in batches. You may still query or read these documents while the results may vary based on the progress made. Bulk delete doesn't delete any documents added or modified after the operation starts.

#### Bulk delete specific collection groups

To bulk delete specific collection groups, use the [`--collection-ids`](https://cloud.google.com/sdk/gcloud/reference/beta/firestore/bulk-delete#FLAGS) flag. The operation deletes only the collection groups with the given IDs.

```
gcloud firestore bulk-delete \
--collection-ids=[COLLECTION_GROUP_ID_1_OR_KIND_1],[COLLECTION_GROUP_ID_2_OR_KIND_2],[SUBCOLLECTION_GROUP_ID_1_OR_KIND_3] \
--database=[DATABASE]
```

For example, to bulk delete the `restaurants` and `reviews` collection groups:

```
gcloud firestore bulk-delete \
--collection-ids=restaurants,reviews \
--database='cymbal'
```

### Manage bulk delete operations

After you start a bulk delete operation, Cloud Firestore assigns the operation a unique name. You can use the operation name to delete, cancel, or check the status of the operation.

#### List all bulk delete operations

Use the [`operations list`](https://cloud.google.com/sdk/gcloud/reference/firestore/operations/list) command to see all running and recently completed operations, including bulk delete operations:

```
gcloud firestore operations list
```

#### Check operation status

Use the [`operations describe`](https://cloud.google.com/sdk/gcloud/reference/firestore/operations/describe) command to show the status of a bulk delete operation.

```
gcloud firestore operations describe [OPERATION_NAME]
```

#### Estimate the completion time

A request for the status of a long-running operation returns the metrics `workEstimated` and `workCompleted`. Divide `workCompleted` by `workEstimated` for a rough progress estimate.

#### Cancel an operation

Use the [`operations cancel`](https://cloud.google.com/sdk/gcloud/reference/firestore/operations/cancel) command to stop an operation in progress:

```
gcloud firestore operations cancel [OPERATION_NAME]
```

#### Delete an operation

Use the [`gcloud firestore operations delete`](https://cloud.google.com/sdk/gcloud/reference/firestore/operations/delete) command to remove a completed operation from the list of recent operations.

### Billing and pricing for managed bulk delete operations

You are required to enable billing for your Google Cloud project before you use the managed bulk delete service. Bulk delete operations are charged for document reads and deletes at the rates listed in [Cloud Firestore pricing](/content/docs/firestore/pricing/index.html).

### View bulk delete costs

Bulk delete operations apply the `goog-firestoremanaged:bulkdelete` label to billed operations. In the [Cloud Billing reports page](https://cloud.google.com/billing/docs/how-to/reports#getting_started), you can use this label to view costs related to bulk delete operations.

## Best practices

[Avoid skipping over deleted data](/content/docs/firestore/best-practices#avoid-skipping/index.html) which might slow down the queries.
