Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

MongoDB\GridFS\Bucket::delete()

MongoDB\GridFS\Bucket::delete()

Delete a file and its chunks from the GridFS bucket.

function delete($id): void
$id : mixed
The _id of the file to delete.

MongoDB\GridFS\Exception\FileNotFoundException if no file was found for the selection criteria.

MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).

If the files collection document is not found, this method will still attempt to delete orphaned chunks.

<?php
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);
$id = $bucket->uploadFromStream('filename', $stream);
$bucket->delete($id);

Back

__construct()

On this page