How to Clear a MQ Queue from a Script or Program

Have you ever wanted to clear a MQ queue? A quick and easy way to do it is to use runmqsc’s CLEAR QLOCAL command. This method works very well as long as no application has the local queue opened for input.

Method #1:

If you need to clear a queue on Linux, Unix, Windows or IBM i (OS/400) then start runmqsc and issue the following MQSC command:

clear ql(TEST.Q.NAME)

Method #2:

On IBM i (OS/400), the user can also use the CLRMQMQ command. To issue the CLRMQMQ command, type the following on the 5250 terminal (use your queue manager and queue names):

CLRMQMQ QNAME(TEST.Q.NAME) MQMNAME(QMgrName)

Method #3:

For Unix / Linux, the following simple shell script will work to clear a queue of a local queue manager:

#!/bin/sh
#
# Clear a MQ queue (clearqueue.sh)
#
# Parameters:
#  $1=Queue manager
#  $2=Queue name
#

if [ $# -ne 2 ] ; then
#  Tell the user what they did wrong!
   echo "Usage:"
   echo "   clearqueue.sh QMgrName QueueName"
   exit;
fi

echo "clear qlocal($2)" | runmqsc $1

exit

To run the Unix/Linux script, open a shell prompt and type the following (use your queue manager and queue names):

clearqueue.sh QMgrName QueueName

Method #4:

For Windows, the following simple batch file will work to clear a queue of a local queue manager:

@echo off

REM Clear a MQ queue (clearqueue.bat)

REM Parameters:
REM  %1=Queue manager
REM  %2=Queue name

if %1. == .  goto help
if %2. == .  goto help

echo clear qlocal(%2) | runmqsc %1
goto theend

rem Tell the user what they did wrong!
:help
echo Usage:
echo    clearqueue.bat QMgrName QueueName
:theend

To run the Windows batch script, open a command prompt and type the following (use your queue manager and queue names):

clearqueue.bat QMgrName QueueName

Method #5:

You can also download and compile the ClearQ program that is listed on the following web page:
http://www.capitalware.biz/mq_code_c.html

Again, this method works very well as long as no application has the queue opened for input.

To run the program on Linux, Unix, Windows, OS/400 or z/OS, open a prompt and type the following (use your queue manager and queue names):

clearq QueueName QMgrName

Method #6:

If you need to clear a queue when another application is reading from the queue (opened for input) then you can download and compile the EmptyQ program that is listed on the following web page:
http://www.capitalware.biz/mq_code_java.html

To run the program on Linux, Unix, Windows, IBM i (OS/400) or z/OS, open a prompt and type the following (use your queue manager and queue names):

java EmptyQ -m QMgrName -h hostname -p port## -c ChannelName -q QueueName

Method #7:

If you need to clear a queue on z/OS then the following JCL will work (use your queue manager and queue names):

//STEP01   EXEC PGM=CSQUTIL,
//            PARM='QMGR',
//            COND=(0,LT)
//STEPLIB  DD DISP=SHR,DSN=HLQ.SCSQANLE
//         DD DISP=SHR,DSN=HLQ.SCSQAUTH
//SYSIN    DD *
COMMAND
//CSQUCMD  DD *
CLEAR QLOCAL(TEST.Q.NAME)
/*
//SYSPRINT DD SYSOUT=*

I hope this posting helps with your message management issues.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in IBM i (OS/400), Java, Linux, Mac OS X, MQ, Open Source, Programming, Unix, Windows, z/OS.

6 Responses to How to Clear a MQ Queue from a Script or Program

  1. Jeferson says:

    Hi,
    I tryied ti use the java EmptyQ, but im having this errors:

    Exception in thread “main” java.lang.NoClassDefFoundError: EmptyQ
    Caused by: java.lang.ClassNotFoundException: EmptyQ
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: EmptyQ. Program will exit.

    Im executing this via DOS, and the EmptyQ.java is in my C:\ (im executing from there).

    Some idea??
    Thanks!!

    • Hello,

      If you do not put the path of your class in the CLASSPATH environment variable then the JVM will not find your class. The typical thing to do is to put a “.” (period), meaning current directory, in the CLASSPATH then you change directory in your DOS prompt and the JVM will find your class.

      Please lookup “how to execute a Java class” in a Java book or online.

      Regards,
      Roger Lacroix
      Capitalware Inc.

  2. zpat says:

    MQ explorer can empty a queue using either the clear method or the get message method.

    So can MO71 although it’s slightly more complex to remove all messages using the get method.