Page.delete

Waldir Pimenta edited this page May 25, 2014 · 1 revision
Clone this wiki locally

Page.delete() is used to delete a page. This requires the "delete" user right, typically only given to site administrators. The page is hidden but remains visible to administrators. Typical usage:

page = site.Pages['User:Apple/Sandbox']
page.delete(reason='Copyright violation')

Associated API: API:Delete

Parameters

  • (optional) reason: Reason for deletion. It is recommended to always include a clear deletion reason. (Default: empty string)
    • The MediaWiki API allows you to omit the reason and generates a default reason. This functionality is not available through mwclient.
  • (optional) watch: If set to True, also add this page to the logged-in user's watchlist. (Default: False)
  • (optional) unwatch: If set to True, and this page is on the logged-in user's watchlist, remove it from the watchlist. (Default: False)
  • (optional) oldimage: Allows deleting an old revision of an file (not the entire file description page) by specifying an oldimage name.
    • oldimage names are obtained from the archivename property of pages returned by Image.imagehistory(). See Examples below.
    • If the page being deleted is not a file description page, this option is ignored.

Result

The page is deleted on the site and an entry is added to the deletion log. The method returns a dict with the following keys:

  • 'title': the canonical title of the deleted page
  • 'reason': the reason for deletion
    • Because mwclient does not currrently support default reasons, this should always match the supplied reason.

Errors

  • InsufficientPermission: The currently logged-in user does not have permission to delete the page.
  • APIError: Depending on the error's code property:
    • 'permissiondenied': The currently logged-in user does not have permission to delete the page. May occur instead of InsufficientPermission in the case of a race condition where the user loses the delete right during the delete() call.
    • 'cantdelete': The page could not be deleted, usually because someone else already deleted it.
    • 'unknown': Depends on error's "info" property. If set to "Unknown error: ``invalidoldimage''", indicates an invalid oldimage argument was passed.
  • APIDisabledError: The site has disabled access through the MediaWiki API.
  • MaximumRetriesExceeded: API call to perform the delete failed and was retried until all retries were exhausted.

Examples

Deleting a page normally, adding the page to the watchlist:

page = site.Pages['User:Apple/Sandbox']
page.delete(reason='Copyright violation', watch=True)

Deletes all file revisions of the file "Test.jpg" except the most recent one:

for page in site.Pages['File:Test.jpg'].imagehistory():
    if page.has_key('archivename'):
        image.delete(reason='Testing', oldimage=page['archivename'])

Because it is always possible that another user may delete the file before mwclient, robust scripts should always catch APIError:

try:
    page = site.Pages['User:Apple/Sandbox']
    page.delete(reason='Copyright violation', watch=True)
except APIError, e:
    if e.code != 'cantdelete': # Okay if already deleted
        raise

This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 16:21, 21 February 2012, and its only editor was Derrickcoetzee (@dcoetzee).