Is there a statement that can drop all stored procedures in MySQL? Alternatively (if the first one is not possible), is there such thing as temporary stored procedures in MySQL? Something similar to temporary tables?
-
Hi, although it sounds funny :)) but it's something I borrowed from MS SQL Server (http://msdn.microsoft.com/en-us/library/aa174792(SQL.80).aspx) which says that "SQL Server 2000 also supports temporary stored procedures that, like temporary tables, are dropped automatically when you disconnect." But my purpose is to have everything dropped after the session, although I still have backup plans like using the language to loop on stored procedures names and drop them one by one (haven't checked if it's possible). – Kenston Choi Jun 12 '10 at 08:07
-
@ebyrob - You're quite right. MySQL doesn't support anonymous blocks of procedural code. My comments reflected an invalid transfer from Oracle DBMS which does permit the execution of arbitrary procedural code without defining permanent stored procs – APC Jan 31 '17 at 17:50
7 Answers
I would have thought that this would do it, but I'm open to corrections:
(EDITED to incorporate a good point provided in the comments)
delete from mysql.proc WHERE db LIKE <yourDbName>;
(As pointed out by Balmipour in the comments below, it's a good idea to specify the database.)
I think it's valid to want to drop all procedures in a given database, otherwise in a long development cycle there's a danger of obsolete procedures and functions accumulating and muddying everything up.

- 836
- 9
- 15
-
-
1@user1070300 I'm afraid THIS WILL DESTROY ALL PROCEDURES, **including MySQL ones**, which aren't related to your DB, and procedures from all bases if you got several on your server. Consider adding a "WHERE db LIKE
" to this request please, since **it looks quite dangerous** as it is. – Balmipour Apr 06 '16 at 21:10 -
@Balmipour A good point, although it doesn't answer the original request any more, which was for a statement that can drop all stored procedures in MySQL. – user1070300 Apr 09 '16 at 09:48
-
Is `mysql.proc` still a viable method of interacting with MySQL meta information in 5.7? This command certainly seems more convenient than reading `information_schema.ROUTINES` and generating dynamic SQL commands. – ebyrob Jan 31 '17 at 14:42
-
1Also, it is a good idea to run a select before any delete you don't trust: `SELECT * FROM mysql.proc WHERE db LIKE 'yourDbName';` – Manatax Apr 11 '17 at 22:59
-
See also https://stackoverflow.com/questions/49276518/mysql-5-7-drop-stored-procedures-via-mysql-proc/49279921#49279921 – JonBrave Mar 14 '18 at 14:15
-
You'd need write privilege to mysql.proc, which is not the case @azure for example. – UNeverNo Mar 03 '20 at 13:18
Since DROP PROCEDURE and DROP FUNCTION does not allow sub selects, I thought it might be possible to perform the operation through another stored procedure, but alas, MySQL does not allow stored procedures to drop other stored procedures.
I tried to trick MySQL to to this anyway by creating prepared statements and thus separating the drop call somewhat from the stored procedure, but I've had no luck.
So therefore my only contribution is this select statement which creates a list of the statements needed to drop all stored procedures and functions.
SELECT
CONCAT('DROP ',ROUTINE_TYPE,' `',ROUTINE_SCHEMA,'`.`',ROUTINE_NAME,'`;') as stmt
FROM information_schema.ROUTINES;

- 143,130
- 81
- 406
- 459

- 4,747
- 2
- 32
- 34
-
5THIS WILL CREATE DROP FUNCTION FOR ALL AVAILABLE DATABASE SCHEMA. USE WITH CAUTION! – Dasun Jun 07 '13 at 16:34
This seems to drop the procedures...not sure about implications though
DELETE FROM mysql.proc WHERE db = 'Test' AND type = 'PROCEDURE';

- 1,033
- 9
- 14
-
See also https://stackoverflow.com/questions/49276518/mysql-5-7-drop-stored-procedures-via-mysql-proc/49279921#49279921 – JonBrave Mar 14 '18 at 14:14
I can almost get working a piece of code to drop all stored procs, but I think MySQL isn't letting me use a LOOP or a CURSOR outside of a stored procedure.
I can write a SQL file that counts the number of stored procedures for a given schema, and I have the code to iterate through the table and drop procedures, but I can't get it to run:
SELECT COUNT(ROUTINE_NAME)
INTO @remaining
FROM information_schema.ROUTINES
WHERE ROUTINE_SCHEMA = SCHEMA()
AND ROUTINE_TYPE = 'FUNCTION';
kill_loop: LOOP
IF @remaining < 1 THEN
LEAVE kill_loop;
END IF;
SELECT ROUTINE_NAME
INTO @cur_func_name
FROM information_schema.ROUTINES
WHERE ROUTINE_SCHEMA = SCHEMA()
AND ROUTINE_TYPE = 'FUNCTION'
LIMIT 1;
DROP FUNCTION @cur_func_name;
@remaining = @remaining - 1;
END LOOP;

- 2,754
- 2
- 24
- 36
-
Thanks for the idea which will help future users! I'm not sure on how this issue on cursor/loop should be addressed... – Kenston Choi Nov 14 '10 at 04:11
-
@Kenston: I eventually wrote a MySQL script that outputs SQL that will drop the procs -- so the output can be piped back into mysql. – Tim has moved to Codidact Jan 10 '11 at 02:20
I use select statement to display all Drop Procedure statements of a specific database in one cell. Then copy it to query window.
SET group_concat_max_len = 4096;
SELECT GROUP_CONCAT(Procedures SEPARATOR '; ') From (SELECT CONCAT(
"DROP PROCEDURE IF EXISTS `",SPECIFIC_NAME, '`') AS Procedures
FROM information_schema.ROUTINES R WHERE R.ROUTINE_TYPE = "PROCEDURE"
AND R.ROUTINE_SCHEMA = 'my_database_name') As CopyThis;

- 10,098
- 5
- 45
- 45
after the select statement which creates a list of the statements needed to drop all stored procedures and functions. You can avoid the manual work of copy pasting the listed queries as follows:
mysql>SELECT
CONCAT('DROP ',ROUTINE_TYPE,' `',ROUTINE_SCHEMA,'`.`',ROUTINE_NAME,'`;') as stmt
FROM information_schema.ROUTINES into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;

- 8,944
- 10
- 43
- 53
-
Thanks for the idea, though this should have been placed as comment to the accepted answer. I actually run this using Java, but this can still be useful for others. – Kenston Choi Apr 11 '12 at 11:29
-
People ignore comments includes me and they are not much noticeable too. – Angelin Nadar Sep 02 '13 at 05:17
@user1070300's answer seems to work, but it looks like it can drop a lot of things.
A quick DESC mysql.proc;
led me to add a WHERE to my request, so as to spare already existing MySQL procedures, which I had, of course, no reason to drop.
I executed DELETE FROM mysql.proc WHERE db NOT LIKE 'mysql'
;
If you have several bases and want to target only one, use DELETE FROM mysql.proc WHERE db LIKE '<yourDbName>'
instead;
Btw, if like me, you are completely clearing your database and need to also drop your tables, you can use this linux shell script :
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done
(see Truncate all tables in a MySQL database in one command? for more details)
-
If you are clearing your entire database, isn't it easier to just drop and re-create your schema? – Manatax Apr 11 '17 at 23:03
-
@manatax It may be, of course, but while I don't remember them, I guess I had good reasons not to use this approach. Missing rights to do so could have been one. Not wanting to mess with other database settings is another. (I doubt users would be affected, but I'm sure some aspects would). I don't play with that enough to give you a more precise answer, bu anyway, the main purpose of my answer was : **Don't blindly delete everything, add a `where` statement !** (I'm glad the answer was edited to take this in account) – Balmipour Apr 12 '17 at 15:21