The DeleteObjectCommand is used to permanently delete an object from your DID Space. This action is irreversible.
This command is the standard method for removing individual files. To delete multiple objects, you must issue a separate DeleteObjectCommand for each object.
Input
The command requires the key of the object you wish to delete.
- key
string(required) — The unique identifier for the object within the space. This is typically the object's path, for example, path/to/your/file.txt.
Output
The output indicates the success of the operation via the HTTP status code. A successful deletion will return a 200 status code.
- statusCode
number— The HTTP status code of the response. A value of 200 indicates success. - statusMessage
string— The HTTP status message corresponding to the status code. - data
void— This field is undefined for a successful DeleteObjectCommand operation.
Usage Example
The following example demonstrates how to initialize a SpaceClient and use DeleteObjectCommand to delete a file named notes.txt from the root directory of your space.
delete-file.ts
import { SpaceClient, DeleteObjectCommand } from '@blocklet/did-space-js';
import getWallet from '@blocklet/sdk/lib/wallet';
async function deleteFile() {
// Initialize the SpaceClient
const client = new SpaceClient({
auth: {
endpoint: 'https://www.didspaces.com/app/api/space/...', // Your space endpoint
wallet: getWallet(),
},
});
// Specify the key of the object to be deleted
const fileKey = 'notes.txt';
// Create and send the delete command
const command = new DeleteObjectCommand({ key: fileKey });
const output = await client.send(command);
// Verify the result
if (output.statusCode === 200) {
console.log(`Successfully deleted object: ${fileKey}`);
} else {
console.error(`Failed to delete object. Status: ${output.statusCode} - ${output.statusMessage}`);
}
}
deleteFile();In this example, we first create an instance of SpaceClient with the necessary authentication details. We then instantiate DeleteObjectCommand, providing the key of the object to be removed. After sending the command, we check the statusCode of the response to confirm that the deletion was successful.
Best Practices and Common Patterns
Confirm Deletion
Since deleting an object is a permanent action, it's crucial to ensure your application logic correctly identifies the object to be deleted. Consider implementing a confirmation step in user-facing applications before executing this command.
Handling "Not Found" Errors
Attempting to delete an object that does not exist will result in an error (typically a 404 Not Found status). Your application should handle this gracefully. You might want to check if an object exists using ListObjectsCommand before attempting to delete it if the object's existence is uncertain. This can help prevent unnecessary API calls and errors.