HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Delete all the data from all tables

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
tablesthedeleteallfromdata

Problem

I have a little problem. I have to delete all the data from our database. I don't want to drop tables, only delete the data sets.

These are 47 tables in total.

I tried the following:

Delete FROM(select TABLE_NAME from USER_TABLES);
Delete FROM(select * from ALL_ALL_TABLES Where OWNER = 'DB_NAME')


But that is not valid. How can I efficiently remove all the data from all my tables?

Solution

Generate the statements you need

select 'DELETE FROM '||table_name||' ;' from user_tables;


Even better would be using TRUNCATE instead of DELETE. That would prevent generating extra redo vectors in the redo log - in the end all you want to do is just get rid of the data. It would also avoid all actions by any DELETE triggers that the tables have.

select 'TRUNCATE TABLE '||table_name||' ;' from user_tables;

Code Snippets

select 'DELETE FROM '||table_name||' ;' from user_tables;
select 'TRUNCATE TABLE '||table_name||' ;' from user_tables;

Context

StackExchange Database Administrators Q#74519, answer score: 7

Revisions (0)

No revisions yet.