Dmitry Tunik Dmitry Tunik - 21 days ago 10
Bash Question

db2 drop few database as once

recently I started to work with db2, and created few databases.

To drop a single DB I should use

db2 drop db demoDB
, is there a way to drop all DBs at once?

Thanks

Answer Source

first the way to see all the databases on your instance is one of the follow:
1. db2 list db directory
2. db2 list active active databases
depends on your need ( all DBs or just the active DBs)
Im sure there is more DBs lists you can get.(at DB2 user guide)

The way I usually drop all my DBs is by using shell script: 1. create new script by using 'vi db2_drop_all.sh' or some other way you want. 2. paste the code: #!/bin/bash -x

for db_name in $(db2 list db directory | grep Database | \
                grep name  | cut -d= -f2); do
        db2 drop db $db_name || true
done
exit 0

3. save changes
4. and just run the script (after you switched to your instance of course ) sh db2_drop_all.sh
Notice that in step 2 you can change the list of DBs as you wish. ( for example to db2 list active databases)

Hope it helped you. :)