# Data deletion

<a id="overview">

## Overview

There are a few ways to different delete data from the server:

* [Collaboration REST API](https://help.cke-cs.com/api/docs#tag/Collaboration/paths/~1collaborations~1%7Bdocument_id%7D/delete) to delete only active collaboration session data.
* [Storage REST API](https://help.cke-cs.com/api/docs#tag/Storage/paths/~1storage~1%7Bdocument_id%7D/delete) to delete document content from the document storage.
* [Suggestions REST API](https://help.cke-cs.com/api/v5/docs#tag/Suggestions/paths/~1suggestions/delete) to delete comments.
* [Comments REST API](https://help.cke-cs.com/api/v5/docs#tag/Comments/paths/~1comments/delete) to delete suggestions.

<a id="document-delete">

## Document delete

As you can see, all the above request deletes only part of the entire document. Depending on your needs, you may want to delete the entire document with all comments, suggestions, and more. To do this you can use [Documents REST API](https://help.cke-cs.com/api/docs#tag/Documents).

> **Warning**
>
> Please remember that this action is irreversible. All document data will be permanently deleted from the database.

<a id="example">

### Example

The example below has been prepared in `Node.js`.

1. Run the following commands

```bash
mkdir cs-document-delete-example && cd cs-document-delete-example && npm init -y && npm i axios && touch delete.cjs
```

2. Open `cs-document-delete-example/delete.cjs` and paste the following code snippet:

```javascript
const crypto = require( 'crypto' );
const axios = require( 'axios' );

// Update with your credentials and application endpoint
const environmentId = 'txQ9sTfqmXUyWU5LmDbr';
const apiSecret = '4zZBCQoPfRZ7Rr7TEnGAuRsGgbfF58Eg0PA8xcLD2kvPhjGjy4VGgB8k0hXn';
const applicationEndpoint = 'https://33333.cke-cs.com';

// Set document id
const documentId = 'my_document_id';

const deleteApiEndpoint = `${ applicationEndpoint }/api/v5/${ environmentId }/documents/${ documentId }`;

( async () => {
	await deleteDocument();

	async function deleteDocument( ) {
		try {
			const config = getConfig( 'DELETE', deleteApiEndpoint );
			const response = await axios.delete( deleteApiEndpoint, config );

			console.log( response.status );
		} catch ( error ) {
			console.log( error.message );
			console.log( error.response.data );
		}
	}
} )();

function getConfig( method, url ) {
	const CSTimestamp = Date.now();

	return {
		headers: {
			'X-CS-Timestamp': CSTimestamp,
			'X-CS-Signature': generateSignature( apiSecret, method, url, CSTimestamp )
		}
	};
}

function generateSignature( secret, method, uri, timestamp, body ) {
	const url = new URL( uri );
	const path = url.pathname + url.search;
	const hmac = crypto.createHmac( 'SHA256', secret );

	hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

	if ( body ) {
		hmac.update( Buffer.from( JSON.stringify( body ) ) );
	}

	return hmac.digest( 'hex' );
}
```

3. Update your credentials, and the `documentId` value in the code snippet with the id of an existing document.

4. Execute the `node delete.cjs` command.

After a successful response with a status code `204` from the delete request, the document with all resources will be deleted from the server.

<a id="troubleshooting">

## Troubleshooting

* We suggest enabling [Insight Panel](../../developer-resources/monitoring/insights-panel.md), where you will find detailed logs from the whole process including exact reasons for any failed requests.

---

Full index of the Cloud Services documentation: [llms.txt](../../../llms.txt)
