curl --request DELETE \
--url https://api.example.com/api/delete-document{
"success": true,
"message": "<string>",
"error": "<string>"
}Delete a document and all its associated vector embeddings
curl --request DELETE \
--url https://api.example.com/api/delete-document{
"success": true,
"message": "<string>",
"error": "<string>"
}curl -X DELETE "https://your-domain.com/api/delete-document?id=42"
{
"success": true,
"message": "Document deleted successfully"
}
{
"success": false,
"error": "Error al eliminar documento"
}
{
"success": false,
"error": "Error al eliminar documento"
}
public function deleteDocument($id)
{
// 1. Fetch document to get file path
$document = $this->getDocument($id);
if (!$document) {
throw new \RuntimeException('Document not found');
}
// 2. Delete physical file
$filepath = $this->uploadPath . '/' . $document['filename'];
if (file_exists($filepath)) {
unlink($filepath);
}
// 3. Delete database record (cascades to vectors)
return $this->db->delete('documents', 'id = :id', [':id' => $id]);
}
documents table, the database automatically deletes all related records in the vectors table through foreign key constraints. This ensures:
$filepath = $this->uploadPath . '/' . $document['filename'];
if (file_exists($filepath)) {
unlink($filepath);
}
$id = $_GET['id'] ?? null;
if (!$id) {
throw new \InvalidArgumentException('Document ID required');
}
| Resource | Action | Location |
|---|---|---|
| Physical File | Deleted from filesystem | {uploadPath}/{filename} |
| Document Record | Deleted from database | documents table |
| Text Chunks | Cascade deleted | vectors table |
| Vector Embeddings | Cascade deleted | vectors table |
const documentIds = [42, 43, 44];
const deletions = documentIds.map(id =>
fetch(`/api/delete-document?id=${id}`, { method: 'DELETE' })
.then(r => r.json())
);
const results = await Promise.all(deletions);
const successful = results.filter(r => r.success).length;
console.log(`Deleted ${successful}/${documentIds.length} documents`);
// First, get all documents
const { documents } = await fetch('/api/get-documents').then(r => r.json());
// Filter documents older than 90 days
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - 90);
const oldDocs = documents.filter(doc =>
new Date(doc.created_at) < cutoffDate
);
// Delete each old document
for (const doc of oldDocs) {
await fetch(`/api/delete-document?id=${doc.id}`, { method: 'DELETE' });
}
console.log(`Cleaned up ${oldDocs.length} old documents`);
const { documents } = await fetch('/api/get-documents').then(r => r.json());
// Find documents larger than 10MB
const largeDocs = documents.filter(doc => doc.file_size > 10 * 1024 * 1024);
console.log(`Found ${largeDocs.length} documents larger than 10MB`);
// Delete after user confirmation
for (const doc of largeDocs) {
await fetch(`/api/delete-document?id=${doc.id}`, { method: 'DELETE' });
}