# remove\_table

##

## Function: `remove_table`

The `remove_table` function is used to delete an existing table from the currently opened database. It removes the table's file from the filesystem and updates the database's metadata to reflect the change.

***

### Declaration

```cpp
operation remove_table(std::string name);
```

### Parameters

* `name` (`std::string`) The name of the table to be removed. The function will look for a file named `<name>.table` in the database directory.

### Return Value

* `operation` An object containing:
  * `stat`: Indicates whether the operation was `SUCCESS` or `FAILED`.
  * `error`: Provides a description of the error if the operation fails.

### Functionality

1. **Checks if the Database is Open** The function verifies if the database is currently open. If the database is not open, the operation fails with an error message stating "Database not open."
2. **Validates Table Existence** The function checks if the table file exists in the database directory. If the file is not found, the operation fails with an error message stating "Table does not exist."
3. **Removes the Table File** If the table file exists, it is deleted from the filesystem using `std::filesystem::remove`.
4. **Updates Database Metadata** After successfully removing the file, the table is removed from the `tables` map, ensuring the database's in-memory metadata is consistent.
5. **Returns the Result** If the table is successfully removed, the operation status is set to `SUCCESS`. Otherwise, an error message is returned describing the failure.

### Example

Here’s a simple example of how to use the `remove_table` function:

```cpp
#include "qic.h"
#include <iostream>

int main() {
    data_base db;

    // Open the database
    db.open_database("mydatabase.db");

    // Remove the table named "users"
    operation op = db.remove_table("users");

    // Check if the operation was successful
    if (op.stat == SUCCESS) {
        std::cout << "Table 'users' removed successfully!" << std::endl;
    } else {
        std::cout << "Failed to remove table: " << op.error << std::endl;
    }

    // Close the database
    db.close();

    return 0;
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hrodebert.gitbook.io/qic-database-ver-1.0.0/database/data_base/remove_table.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
