Page 1 of 1

VestaCP api delete database

Posted: Wed Mar 17, 2021 12:20 am
by xinaidao
Hello guys.

I need, through an api, to create and delete mysql database.
From what I've been seeing in the documentation, it is possible to create a database via api as shown in the link https://vestacp.com/docs/api/#add_database

My question is whether it is possible to delete the mysql database through this same api. Would anyone know anything about that?
Thank you all

Re: VestaCP api delete database

Posted: Wed Mar 17, 2021 1:28 am
by grayfolk
xinaidao wrote:
Wed Mar 17, 2021 12:20 am
Hello guys.

I need, through an api, to create and delete mysql database.
From what I've been seeing in the documentation, it is possible to create a database via api as shown in the link https://vestacp.com/docs/api/#add_database

My question is whether it is possible to delete the mysql database through this same api. Would anyone know anything about that?
Thank you all
Sure, this is possible. I think you can use this code:

Code: Select all

<?php
// Server credentials
$vst_hostname = 'server.vestacp.com';
$vst_username = 'admin';
$vst_password = 'p4ssw0rd';
$vst_returncode = 'yes';
$vst_command = 'v-delete-database';

$username = 'demo';
$db_name = 'demo_wordpress';

// Prepare POST query
$postvars = array[
    'user' => $vst_username,
    'password' => $vst_password,
    'returncode' => $vst_returncode,
    'cmd' => $vst_command,
    'arg1' => $username,
    'arg2' => $db_name
];
$postdata = http_build_query($postvars);
Note: when you create database it will be created with username prefix: demo_wordpress in this case. For delete database you should use db name with prefix.

Also, see scripts in /usr/local/vesta/bin folder - you can use all those commands via API.

Re: VestaCP api delete database

Posted: Wed Mar 17, 2021 2:29 pm
by xinaidao
Grayflolk,

thank you so much