Delete
Permanently deletes files, making them no longer downloadable.
Signature
async delete(pieceCid: string, options?: DeleteOptions): Promise<DeleteResult>Description
The delete function removes files from the permissions registry and revokes all access permissions. This operation is permanent and cannot be undone. Only the file owner can delete files. Note that the file data remains on Filecoin - this function removes access permissions, not the underlying data.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
pieceCid | string | Yes | Filecoin piece CID of the file to delete |
options | DeleteOptions | No | Delete configuration options |
DeleteOptions
interface DeleteOptions {
debug?: boolean; // Enable debug logging
force?: boolean; // Force delete even with active shares
onProgress?: (progress: DeleteProgress) => void; // Progress callback
}DeleteProgress
interface DeleteProgress {
message: string;
step?: number;
total?: number;
}Returns
Promise<DeleteResult> - A Promise that resolves to an object with the following properties:
| Property | Type | Description |
|---|---|---|
transactionHash | string | Hash of the deletion transaction |
sharesRevoked | number | Number of shares that were revoked |
Examples
Basic Delete Operation
import { Synapse } from '@filoz/synapse-sdk';
import { SynapseStorageSDK } from '@keypo/synapse-storage-sdk';
import { ethers } from 'ethers';
// Initialize wallet and provider
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
const provider = new ethers.JsonRpcProvider('https://api.calibration.node.glif.io/rpc/v1');
const signer = wallet.connect(provider);
// Initialize Synapse
const synapse = await Synapse.create({
signer,
withCDN: true,
});
// Initialize SDK
const sdk = new SynapseStorageSDK(synapse, {
network: 'calibration',
rpcUrl: 'https://api.calibration.node.glif.io/rpc/v1',
encryption: {
registryAddress: '0x8370eE1a51B5F31cc10E2f4d786Ff20198B10BBE',
validationAddress: '0x35ADB6b999AbcD5C9CdF2262c7190C7b96ABcE4C',
bundlerRpcUrl: 'https://rpc.zerodev.app/api/v3/YOUR_PROJECT_ID/chain/84532'
},
storage: {
withCDN: true
}
}, process.env.PRIVATE_KEY);
// Delete file
const pieceCid = 'bafk2bzacedt7j7lnpxvzfdks7ooqvzf2lgjklhkh...';
const result = await sdk.delete(pieceCid, {
onProgress: (progress) => {
console.log(progress.message);
}
});
console.log('File deleted successfully!');
console.log('Transaction Hash:', result.transactionHash);
console.log('Shares revoked:', result.sharesRevoked);With Progress Tracking and Error Handling
async function deleteFileWithVerification(pieceCid) {
try {
// Check file details before deletion
const files = await sdk.list();
const fileToDelete = files.find(f => f.pieceCid === pieceCid);
if (fileToDelete) {
console.log(`File found: ${fileToDelete.fileName || 'Unnamed file'}`);
console.log(`Size: ${fileToDelete.fileSize || 'Unknown'} bytes`);
console.log(`Access: ${fileToDelete.isPublic ? 'Public' : 'Private'}`);
} else {
console.log('File not found in your account');
}
// Delete with options
const result = await sdk.delete(pieceCid, {
debug: true,
force: false, // Don't force delete if there are active shares
onProgress: (progress) => {
console.log(`${progress.message}`);
if (progress.step && progress.total) {
console.log(`Step ${progress.step} of ${progress.total}`);
}
}
});
console.log('Deletion completed!');
console.log('Transaction Hash:', result.transactionHash);
if (result.sharesRevoked > 0) {
console.log(`${result.sharesRevoked} shares were revoked`);
}
// Verify deletion
const filesAfter = await sdk.list();
const deletedFile = filesAfter.find(f => f.pieceCid === pieceCid);
if (!deletedFile) {
console.log('Verification: File successfully removed from account');
} else {
console.log('Warning: File still appears in account');
}
} catch (error) {
if (error.message.includes('not found')) {
console.error('Error: File not found or you may not own it');
} else if (error.message.includes('permission') || error.message.includes('owner')) {
console.error('Error: You can only delete files that you own');
} else if (error.message.includes('active shares')) {
console.error('Error: File has active shares. Use force: true to override');
} else {
console.error('Deletion failed:', error.message);
}
throw error;
}
}
// Usage
await deleteFileWithVerification('bafk2bzacedt7j7lnpxvzfdks7ooqvzf2lgjklhkh...');Notes
- Only the file owner can delete files
- This operation is permanent and irreversible - once deleted, files cannot be recovered
- The file data remains on Filecoin - this function removes access permissions, not the underlying data
- Use list to find piece CIDs for files you want to delete
- Progress tracking shows transaction status and completion details
- Use the
forceoption to delete files even when they have active shares
See Also
- list - For finding piece CIDs of your uploaded files
- upload - For uploading and encrypting files
- download - For downloading and decrypting files
- share - For sharing access to private files
- configuration - SDK configuration options