Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:39:22+00:00 2026-05-27T02:39:22+00:00

I’m trying to write a command that will delete all functions in a namespace.

  • 0

I’m trying to write a command that will delete all functions in a namespace. I’ve already found a command that will generate the drop functions script:

SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname || '('
     || oidvectortypes(proargtypes) || ');'
FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid)
WHERE ns.nspname = 'public'  order by proname;

Source: http://www.postgresonline.com/journal/archives/74-How-to-delete-many-functions.html

This will generate something like:

                 ?column?                 
------------------------------------------
 DROP FUNCTION public.function1(bigint);
 DROP FUNCTION public.function2();
 DROP FUNCTION public.function3(text);

However, I can’t figure out how to change the code, so that the functions are actually deleted – as opposed to only generating the commands.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:39:23+00:00Added an answer on May 27, 2026 at 2:39 am

    The system catalogs changed in Postgres 11! (prokind instead of proisagg) See:

    • How to drop all of my functions in PostgreSQL?

    Could look like this:

    CREATE OR REPLACE FUNCTION public.f_delfunc(_schema text, _del text = '')
      RETURNS text AS
    $func$
    DECLARE
       _sql   text;
       _ct    text;
    BEGIN
       SELECT INTO _sql, _ct
              string_agg('DROP '
                       || CASE p.proisagg WHEN true THEN 'AGGREGATE '
                                                    ELSE 'FUNCTION ' END
                       || quote_ident(n.nspname) || '.' || quote_ident(p.proname)
                       || '('
                       || pg_catalog.pg_get_function_identity_arguments(p.oid)
                       || ')'
                        , E'\n'
              )
              ,count(*)::text
       FROM   pg_catalog.pg_proc p
       LEFT   JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
       WHERE  n.nspname = _schema;
       -- AND p.proname ~~* 'f_%';                     -- Only selected funcs?
       -- AND pg_catalog.pg_function_is_visible(p.oid) -- Only visible funcs?
    
       IF _ct = '0' THEN
          RETURN 'Found 0 functions to delete';
       ELSIF lower(_del) = 'del' THEN                        -- Actually delete!
          EXECUTE _sql;
          RETURN _ct || E' functions deleted:\n' || _sql;
       ELSE                                               -- Else only show SQL.
          RETURN _ct || E' functions to delete:\n' || _sql;
       END IF;
    END
    $func$  LANGUAGE plpgsql;
    

    Call to show:

    SELECT f_delfunc('public');         -- 2nd parameter is covered by default.
    

    Call to delete:

    SELECT f_delfunc('public','del');
    

    Major points

    • You need dynamic SQL for that. Use a plpgsql function or a DO statement (PostgreSQL 9.0+) with EXECUTE.

    • Note the use of the functions pg_get_function_identity_arguments() and pg_function_is_visible. The latter can be omitted. It’s a safeguard so you don’t delete functions outside of the current user’s search_path.

    • I added a “safe mode”. Only delete if $2 = 'del'. Else only show generated SQL.

    • Be aware that the function will delete itself if it lives in the schema you delete from.

    • I also added quote_ident() to safeguard against SQLi. Consider the following:

    CREATE FUNCTION "; DELETE FROM users;"()
      RETURNS int AS
    'SELECT 1'
      LANGUAGE sql;
    
    • This fails if there are dependencies on any involved function. May be resolved by adding CASCADE, but I did not do that here, since it makes the function more dangerous, yet.

    Related:

    • DROP FUNCTION without knowing the number/type of parameters?
    • How to get function parameter lists (so I can drop a function)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.