website/docs/maintenance/manually_fixing_data.mdx
:::note
If you are using docker-compose, you are using Docker Compose v1, which has been deprecated. Docker Compose commands refer to Docker Compose v2. Consider upgrading your docker setup, see Migrate to Compose V2
:::
First you need to find out the ID of the drive or charge:
import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
<Tabs defaultValue="drive" groupId="type" values={[ { label: 'Drive', value: 'drive', }, { label: 'Charge', value: 'charge', }, ]}> <TabItem value="drive">
Drives dashboard and click on the start date of the drive.&var-drive_id=9999.Charges dashboard and click on the start date of the charge.&var-charging_process_id=9999.If for some reason a drive or charge hasn't been fully recorded, for example due to a bug or an unexpected restart, you can terminate it manually. Among other things, this assigns an end date to the drive/charge.
Replace 9999 with the actual ID then run the command while the TeslaMate container is running:
<Tabs defaultValue="drive" groupId="type" values={[ { label: 'Drive', value: 'drive', }, { label: 'Charge', value: 'charge', }, { label: 'Drive (NixOS)', value: 'drive-nixos', }, { label: 'Charge (NixOS)', value: 'charge-nixos', }, ]}> <TabItem value="drive">
docker compose exec teslamate bin/teslamate rpc \
"TeslaMate.Repo.get!(TeslaMate.Log.Drive, 9999) |> TeslaMate.Log.close_drive()"
docker compose exec teslamate bin/teslamate rpc \
"TeslaMate.Repo.get!(TeslaMate.Log.ChargingProcess, 9999) |> TeslaMate.Log.complete_charging_process()"
teslamate-close-drive 9999
teslamate-close-charge 9999
If for some reason a drive or charge was recorded incorrectly, you can delete it.
<Tabs defaultValue="drive" groupId="type" values={[ { label: 'Drive', value: 'drive', }, { label: 'Charge', value: 'charge', }, { label: 'Drive (NixOS)', value: 'drive-nixos', }, { label: 'Charge (NixOS)', value: 'charge-nixos', }, ]}> <TabItem value="drive">
Attach to the running database container:
docker compose exec database psql teslamate teslamate
:::note
If you get the error No such service: database, update your docker-compose.yml or use db instead of database in the above command.
:::
Afterwards replace 9999 with the actual ID then run the query:
```sql
DELETE FROM drives WHERE id = 9999;
```
Attach to the running database container:
docker compose exec database psql teslamate teslamate
:::note
If you get the error No such service: database, update your docker-compose.yml or use db instead of database in the above command.
:::
Afterwards replace 9999 with the actual ID then run the query:
DELETE FROM charging_processes WHERE id = 9999;
You can use the idiomatic maintenance script:
```bash
teslamate-delete-drive 9999
```
You can use the idiomatic maintenance script:
```bash
teslamate-delete-charge 9999
```
NOTE: Always backup your data before performing any database changes.
Connect to your running TeslaMate database
docker compose exec database psql teslamate teslamate
:::note
If you get the error No such service: database, update your docker-compose.yml or use db instead of database in the above command.
:::
Identify the right car ID in the database to remove
SELECT id, vin FROM cars;
num with the ID from the previous command.DELETE FROM cars WHERE id = num;
DELETE FROM car_settings WHERE id = num;
DELETE FROM charging_processes WHERE car_id = num;
DELETE from charges WHERE charging_process_id in (select id from charging_processes where car_id = num);
DELETE FROM drives WHERE car_id = num;
DELETE FROM positions WHERE car_id = num;
DELETE FROM states WHERE car_id = num;
DELETE FROM updates WHERE car_id = num;
NOTE: If your database experiences a lot of updates or deletions, like importing data from other sources or deleting a loaner car or some other similar situation, you might encounter index bloat, which can degrade performance. In such cases, reindexing could be beneficial.
In summary, you don't necessarily need to run REINDEX periodically unless your database workload involves significant amounts of updates and deletions, if so, you may you may proceed as indicated in the following steps:
Connect to your running TeslaMate database
docker compose exec -T database psql teslamate teslamate
:::note
If you get the error No such service: database, update your docker-compose.yml or use db instead of database in the above command.
:::
Run the following script (copy and paste)
REINDEX DATABASE teslamate
Exit the prompt by typing:
\q (or press CTRL + C)