Delete documents#

In this tutorial we will delete data from Dash Platform. Data is stored in the form of documents which are encapsulated in a state transition before being submitted to DAPI.

Prerequisites#

Code#

document-delete.mjs#
import { setupDashClient } from '../setupDashClient.mjs';

const { sdk, keyManager } = await setupDashClient();
const { identity, identityKey, signer } = await keyManager.getAuth();

// Default tutorial contract (testnet). Replace or override via DATA_CONTRACT_ID.
const DATA_CONTRACT_ID =
  process.env.DATA_CONTRACT_ID ||
  'FW3DHrQiG24VqzPY4ARenMgjEPpBNuEQTZckV8hbVCG4';

// Replace with your existing document ID
const DOCUMENT_ID = process.env.DOCUMENT_ID || 'YOUR_DOCUMENT_ID';

try {
  // Delete the document from the platform
  await sdk.documents.delete({
    document: {
      id: DOCUMENT_ID,
      ownerId: identity.id,
      dataContractId: DATA_CONTRACT_ID,
      documentTypeName: 'note',
    },
    identityKey,
    signer,
  });

  console.log('Document deleted successfully');
} catch (e) {
  console.error('Something went wrong:\n', e.message);
}

What’s happening#

After we initialize the client, we get the auth key signer from the key manager. We then call sdk.documents.delete() with an object identifying the document to delete — its id, ownerId, dataContractId, and documentTypeName — along with the signing credentials.

Internally, the method creates a State Transition containing the document deletion instruction, signs the state transition, and submits it to DAPI. Only the document’s owner can delete it.

Note

You do not need to retrieve the full document before deleting it. The sdk.documents.delete() method only requires the document’s identifying fields (id, ownerId, dataContractId, documentTypeName).

Tip

See this in an example app: Dashnote — Delete a note and DashMint Lab — Burn a card.