Skip to main content

Description

Deletes a file or directory from the sandbox filesystem. Use with caution as this operation is irreversible.

Method Signature

async deleteFile(
  path: string,
  sessionId?: string
): Promise<DeleteFileResult>

Parameters

path
string
required
Absolute path to the file or directory to delete
sessionId
string
Session ID to use for this operation. If not provided, the default session is used.

Returns

DeleteFileResult
object
Result of the delete operation
success
boolean
Whether the file or directory was deleted successfully
path
string
Path to the file or directory that was deleted
timestamp
string
ISO 8601 timestamp of when the operation completed
exitCode
number
Exit code from the underlying delete command (0 = success)

Examples

Delete a file

const result = await sandbox.deleteFile('/workspace/temp.txt');
console.log(result.success); // true

Delete a directory

// Deletes directory and all its contents
await sandbox.deleteFile('/workspace/old-project');

Safe deletion with existence check

const exists = await sandbox.exists('/workspace/temp.txt');
if (exists.exists) {
  await sandbox.deleteFile('/workspace/temp.txt');
  console.log('File deleted');
} else {
  console.log('File does not exist');
}

Delete multiple files

const filesToDelete = [
  '/workspace/file1.txt',
  '/workspace/file2.txt',
  '/workspace/file3.txt'
];

for (const file of filesToDelete) {
  await sandbox.deleteFile(file);
}

Cleanup temporary files

// List and delete all .tmp files
const result = await sandbox.listFiles('/workspace', { recursive: true });
const tempFiles = result.files.filter(
  file => file.type === 'file' && file.name.endsWith('.tmp')
);

for (const file of tempFiles) {
  await sandbox.deleteFile(file.absolutePath);
}
console.log(`Deleted ${tempFiles.length} temporary files`);

Error Handling

The method throws an error if:
  • The file or directory does not exist
  • Insufficient permissions to delete
  • The path is invalid or protected
try {
  await sandbox.deleteFile('/workspace/important.txt');
} catch (error) {
  console.error('Failed to delete file:', error.message);
}

Notes

  • Deletion is permanent - there is no recycle bin or undo
  • Deleting a directory removes all its contents recursively
  • Protected system paths cannot be deleted
  • Use exists to check before deleting if needed

See Also

Build docs developers (and LLMs) love